0
def str_to_bin(user_input):
    str_list = list(user_input)

str_to_bin = ('Hello World')

The string 'Hello World' has been turned into a list, so that each character is seperated (because using the replace function in strings only replaces words). But from here on, I have no idea how to change the letter 'ah' to, for example, '000001'. I tried multiple ways but nothing seems to work.

And I want a compact way too, because, obviously converting phrases into binary requires a value for each character.

If doing it with a list isnt the best way to go, how can you replace individual characters in strings?

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
J. Doe
  • 35
  • 1
  • 4

4 Answers4

0
>>> myString = "Hello World"
>>> myString.replace("H","F")
"Fello World"

If you want binary to char (actually, binary to int to char here)

>>> replaceChar = '00010001' #8 bits
>>> int(replaceChar, 2)
17
>>> chr(int(replaceChar, 2))
'\x11'

The replace function is a string method. What exactly has not been working when you try it, and what have you tried?

Harrichael
  • 395
  • 3
  • 11
0

I'm not quite sure what you are asking; however, if you're trying to say you want to go through your list and replace values with other specific values (ex: Say for example replace letters like "a" with their binary values "01100001") then you could use a dictionary and then just process your way through it. Here's an example I made for you using my binary example:

dictionary = {
    'a': "01100001",
    'b': "01100010",
    'c': "01100011",
    'd': "01100100",
    #etc..
    }
def modify(raw_input):
    message = ''
    print("Your new output is: ")
    for character in raw_input:
        message += "%s" % (dictionary[character])
    print message

def main():
    modify(raw_input())
main()

Edit: Input and output for this file would be:

>>> abc
>>> Your new output is: 
>>> 011000010110001001100011
Vale
  • 1,003
  • 1
  • 9
  • 22
0

I think this is what you are looking for but additional clarification is needed. The function converts each character in the string to a binary value.

def str_to_bin(user_input):
    str_list = list(user_input)
    return [format(ord(x), 'b') for x in str_list]

print str_to_bin('Hello World')

# OUTPUT
# ['1001000', '1100101', '1101100', '1101100', '1101111', '100000', '1010111', '1101111', '1110010', '1101100', '1100100']
Andrew
  • 3,711
  • 2
  • 20
  • 17
0

I am not clear on what exactly your requirement is:- whether to return the bianry value of each characters in the input string as a list or to return the equivalent bianry representation of the whole string. That is :- if you provide input abc, you want to return each binary value separately in list as ['1100001', '1100010', '1100011'] or to return the equivalent binary representation 110000111000101100011. However, I think that you can do by your own, once you have the way.

But, as mentioned in your code and by @afarber1, you don't even need to convert the input string to list separately. So the following line is not at all needed :-

str_list = list(user_input)

Because, string is treated as list of characters in Python and you can access over each characters of the string as well as iterate, using the indexes.

def str_to_bin(user_input):

    # if you need binary of each character in list 
    return [format(char, 'b') for char in bytearray(user_input)]

    # if you need equivalent binary representation of the string itself
    return ''.join(format(char, 'b') for char in bytearray(user_input))
Pabitra Pati
  • 457
  • 4
  • 12