String Format byte order

Just so you don’t scratch your head for too long on this:

s = "eRicep1 32"
s = ''.join(c for pair in zip(s[1::2], s[::2]) for c in pair)

s[1::2] and s[::2] will produce strings with even or odd characters ([1::2] = start from 1, stop at the end, step 2),
zip will take one character from each,
then it’s simply a matter of putting things in the right order.
So, with "eRicep1 32", s[1::2] will be "Rcp 2" and s[::2] "eie13"
zip("Rcp 2", "eie13") produces the pairs [('R', 'e'), ('c', 'i'), ('p', 'e'), (' ', '1'), ('2', '3')]
and iterating through those pairs and picking single extracts them from the tuples to produce the list ['R', 'e', 'c', 'i', 'p', 'e', ' ', '1', '2', '3']
All that’s left to do is join them on nothing with ''.join()

There might be libraries with builtins for this, but, well…

1 Like