how to separate values with spaces in for loop

zeurosis :

So I have this program that gives the binary values of the ascii values plus 1 of a user input string.

text = input()
bitString = ''
for ch in text:
    new = ord(ch) + 1

    decimal = new
    while decimal > 0:
        remainder = decimal % 2
        decimal = decimal // 2
        bitString = str(remainder) + bitString

print(bitString)

If the user inputs "abcde" the output will be

11001101100101110010011000111100010

How do I get the binary values to be separated by spaces, where the output would be

1100010 1100011 1100100 1100101 1100110

?

Harsha Biyani :

Very slight change in your code to add space:

text = input()
bitString = ''
for ch in text:
    new = ord(ch) + 1

    decimal = new
    while decimal > 0:
        remainder = decimal % 2
        decimal = decimal // 2
        bitString = str(remainder) + bitString

    bitString = " " + bitString

print(bitString)

Output:

1100010 1100011 1100100 1100101 1100110

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25620&siteId=1