Python Forum

Full Version: [HELP] string into binary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello everyone,
after trying for hours and not finding a solution, i came here to ask for help.

i have a user input a string, for example : something

I have a binary string :

2007d010000000000000000000000000000000000003c3f786d6c2

i want some of the zeros to be rewritten to include the string that the user has typed:

2007d01000000000000000000000000000something3c3f786d6c2

the string must not get longer cause it will break everything, if the user types a bigger string, more of the zeros has to be rewritten, for example :

2007d0100000000000000000000thisisabigstring3c3f786d6c2
What have you tried?

Also, out of curiosity, what is this for?
Do you know how many trailing characters you need to keep? This code assumes it is always 11. It also assumes the replacement characters will always fit in the source string.
s1 = '2007d010000000000000000000000000000000000003c3f786d6c2'
s2 = 'something'
trailing = 11

start = len(s1)-len(s2)-trailing
s3 = ''.join((s1[:start], s2, s1[-trailing:]))
print(s1)
print(s3)
Output:
2007d010000000000000000000000000000000000003c3f786d6c2 2007d01000000000000000000000000000something3c3f786d6c2
This is not "string into binary" but rather making a new string by replacing some characters in a string with characters from another string.