EN
Python - reverse a string
0 points
In this article, we would like to show you how to reverse a string in Python.
Quick solution:
xxxxxxxxxx
1
reversed_string = string[::-1]
Where the statement [::-1]
means:
- start at the end of the string,
- end at position 0,
- move with the step
-1
(step backwards)
In this example, we use a slice that steps backwards to reverse the string.
xxxxxxxxxx
1
string = "ABC"
2
reversed_string = string[::-1]
3
4
print("Original string:", string)
5
print("Reversed string:", reversed_string)
Output:
xxxxxxxxxx
1
Original string: ABC
2
Reversed string: CBA