Search code examples
pythonpython-3.xstringtextreverse

How do this variant of reverse at python?


example at command line linux

echo fc67e62b625f33d7928fa1958a38a353085f0097cc22c08833850772035889b8 | grep -o .. | tac | paste -sd '' -

I try find how do this at python but notting . what i am trying to do - i try reverse every line at text by this rule. Only what i find is

txt = "Hello World"[::-1]
print(txt)

example ABCDEF

echo ABCDEF | grep -o .. | tac | paste -sd '' -

will give EFCDAB

this

txt = "ABCDEF"[::-1]
print(txt)

return FEDCBA


Solution

  • If I understand correctly, you want to split the string to pairs of two characters, and then reverse the order of those pairs.

    You can use re to split the string to pairs like that, and then use the slicing syntax to reverse their order, and join them again:

    import re
    source = 'ABCDEF'
    result = ''.join(re.split('(.{2})', source)[::-1])