1

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

2
  • That means step through the entire string of characters backwards and return the substring. The first and second params are the start and end indices. The third param is the step; which is -1. This tells us to start at the end. Commented Sep 8, 2020 at 18:31
  • But how do what at linux comand line , I can not understand. update qustion what return linux command line Commented Sep 8, 2020 at 18:55

3 Answers 3

1

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])
Sign up to request clarification or add additional context in comments.

1 Comment

Yes this exacly what i was try to do.
1

I think this is an appropriate solution:

sample_string = "ABCDEFG"
string_length = len(sample_string)
reversed_string = sample_string[string_length::-1]
print(reversed_string)

Comments

1

You can do this all with a single line with list comprehension.

chunkReverse = lambda s, n: ''.join([s[i:i+n] for i in range(0, len(s), n)][::-1])

print(chunkReverse('ABCDEF', 2)) # EFCDAB

Explanation

  1. The following function will chunk the string into partitions of size 2.
result = [s[i:i+n] for i in range(0, len(s), n)] # [ 'AB', 'CD', 'EF' ]
  1. Next, it will reverse the order of the tokens.
result = result[::-1] # [ 'EF', 'CD', 'AB' ]
  1. Finally, it will join them back together.
''.join(result) # EFCDAB

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.