I want to work on a binary number which I will enter as a string with raw_input(). I want to flip the ith digit of this number like from 0 to 1 or 1 to 0. Since the string is not mutable I want to convert it into something that can be mutated like a binary digit so I can to an assignment like s[i] = ~s[i]. I know i can do that with string slicing but I want to work with more data types.
Asked
Active
Viewed 66 times
-1
-
2It's easily possible to [replace a single character in a string](http://stackoverflow.com/questions/1228299/change-one-character-in-a-string-in-python) in Python. Is there anything else that needs to be done here? – Anderson Green Dec 24 '16 at 19:37
-
Possible duplicate of [Mutable strings in Python](http://stackoverflow.com/questions/10572624/mutable-strings-in-python) – Moinuddin Quadri Dec 24 '16 at 19:40
1 Answers
0
You may want to use one of the relatively-new data types bytearray or memoryview.
Bytearray is close to what you want: it can hold a string but the structure is mutable so you can easily change one element. Its main weakness for what you want is that it is considered to be an array of short integers, not of characters. So do your casting from character to integer as appropriate.

Rory Daulton
- 21,934
- 6
- 42
- 50