I want to swap each pair of characters in a string. \'2143\' becomes \'1234\', \'badcfe\' becomes \'abcdef\'.
\'2143\'
\'1234\'
\'badcfe\'
\'abcdef\'
How
Here's one way...
>>> s = '2134' >>> def swap(c, i, j): ... c = list(c) ... c[i], c[j] = c[j], c[i] ... return ''.join(c) ... >>> swap(s, 0, 1) '1234' >>>