Q:

How can i change the position of substring within a string in Python?

For example, I have this into a .txt:
xxxxxxxx,yyyyyyyyyyyy,zzzzzzzzz
How can i change the positions of y and z?
For example:
xxxxxxxx,zzzzzzzzz,yyyyyyyyyyyy

A:

You can split the string into a list of substrings:
>>> s = 'xxxxxxxx,yyyyyyyyyyyy,zzzzzzzzz'

>>> parts = s.split(',')
>>> parts
['xxxxxxxx', 'yyyyyyyyyyyy', 'zzzzzzzzz']

Then you can re-order:
>>> reordered = parts[0] + parts[2] + parts[1]
>>> reordered
['xxxxxxxx', 'zzzzzzzzz', 'yyyyyyyyyyyy']

And rejoin:
>>> ','.join(reordered)
'xxxxxxxx,zzzzzzzzz,yyyyyyyyyyyy'

