python text string-by- character reversal and word-wise reversal
Scene:
String-by-character reversal and word-wise reversal
first, the string is reversed by character-by-word, python provides a very useful slice, so you just need a sentence to get it done.
>>>a=' abc EDF DEGD '
>>>a[::-1]
' dged fde CBA '
>>>
And then we look at the word reversal.
1. Similarly, we can also use slices
>>>a=' abc EDF DEGD '
>>>a.split () [::-1]
[' Degd ', ' EDF ', ' abc ']
2. native methods can be used reverse
>>>a=' abc EDF DEGD '
>>>Result=a.split ()
>>>result
[' abc ', ' EDF ', ' Degd ']
>>>Result.reverse ()
>>>Result
[' Degd ', ' EDF ', ' abc ']
>>>result="'. Join (Result)
>>>result
' degd EDF ABC '
>>>
in the process of inversion, I accidentally found Join another way to use
>>>a=' ABCD '
>>>"'. Join (a)
' a b c d '
>>>a=' abc EDF DEGD '
>>>"'. Join (a)
' A B c e d f d e G d '
>>>
It can quickly re-add each character to the middle of our assigned character.
>>>' + '. Join (a)
' a+b+c+ +e+d+f+ +d+e+g+d '
>>>
All in all, or use the method of slicing the best, the most recommended to use
Python text string-by-character inversion and word-wise reversal