+ 3
Is there any function like reverse() in python to reverse a given string??
7 Answers
+ 8
use slicing:
reversed = my_string[::-1]
+ 7
Jan Markus ,
i was not aware of the *unpack operator together with reversed(),
thanks!
+ 6
Jan Markus ,
revesed() is working for strings. it looks not as elegant as the slice, but just for information:
txt = "This is Major Tom to Ground Control, i'm stepping through the door"
print("".join(reversed(txt)))
+ 6
[::-1] is faster than reversed()
+ 3
+ 3
Jaweed it works as you said, but there's no need to put -1 at first slice argument ^^ (see the first post of this thread)
+ 2
Python String Slice
s="Monty Python"
print(s[-1: :-1]) # reversed all string