I want to swap each pair of characters in a string. '2143' becomes '1234', 'badcfe' becomes 'abcdef'.
'2143'
'1234'
'badcfe'
'abcdef'
How can I do this in Python?
oneliner:
>>> s = 'badcfe' >>> ''.join([ s[x:x+2][::-1] for x in range(0, len(s), 2) ]) 'abcdef'
1.4m articles
1.4m replys
5 comments
57.0k users