Ask Your Question

Revision history [back]

You could just use Python string replacement: https://docs.python.org/2/library/stdtypes.html#str.replace. Replace every "0" with "x", then replace every "1" with "0", then replace every "x" with "1". Or use the translate method: https://docs.python.org/2/library/stdtypes.html#str.translate: create a translation table swapping 0 and 1 and then apply it. In Python 2:

sage: import string
sage: T = string.maketrans('01', '10')
sage: s = '00111'
sage: s.translate(T)
'11000'

In Python 3:

sage: T = str.maketrans({'0': '1', '1': '0'})
sage: s = '00111'
sage: s.translate(T)
'11000'