So I am wondering if a binary string such as:
01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001
(Hello Mom!)
can be reversed in the way that ALL 1s turn into 0s and vice verca in Python 3.
You can split the binary string and use bitwise manipulation, e.g.:
In []:
x = '01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001'
r = [format(int(n, 2) ^ 0xff, 'b') for n in x.split()]
r
Out[]:
['10110111', '10011010', '10010011', '10010011', '10010000',
'11011111', '10110010', '10010000', '10010010', '11011110']
It's simple enough to join this back up:
In []:
' '.join(r)
Out[]:
'10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110'
You can use join with a generator, and replace the '1' with '0's and vice versa, while keeping any character that is neither (a space in your example) unchanged:
string = "01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001"
reverse = ''.join('0' if c=='1' else ('1' if c=='0' else c) for c in string)
print(reverse)
Outputs:
10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110
As commented by @AChampion, you could shorten and improve the above code using:
string = "01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001"
reverse = ''.join('10'[int(c)] if c in '01' else c for c in string)
print(reverse)
str.translate() is very useful, and efficient, for this:
bits = '01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001'
table = {ord('0'): '1', ord('1'): '0'} # '0' -> '1', '1' -> '0'
inverted_bits = bits.translate(table)
print(inverted_bits)
Output:
10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110
This is also handy if you wanted to apply other transformations. For example the spaces could also be removed with this translation table:
table = {ord('0'): '1', ord('1'): '0', ord(' '): None}
print(bits.translate(table))
Output:
10110111100110101001001110010011100100001101111110110010100100001001001011011110
Also, use str.maketrans() to create the translation table:
>>> table = str.maketrans('01', '10')
>>> print(bits.translate(table))
10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110
or
>>> print(bits.translate(str.maketrans('01', '10', ' ')))
10110111100110101001001110010011100100001101111110110010100100001001001011011110
a="01001000 01100101 01101100 01101100 01101111 00100000 01001101 01101111 01101101 00100001"
a=a.replace('0','a').replace('1','0').replace('a','1')
You could use the replace function.
Output:
'10110111 10011010 10010011 10010011 10010000 11011111 10110010 10010000 10010010 11011110'
str.translate() to avoid any "workaround" which requires that the string be processed 3 times.
xoror^operator.xordoesn't work on a string.int(n, 2) ^ 0xff.^on a string, and I pointed out that will not work.