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:
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)
Practical example
In this example, we use a slice that steps backwards to reverse the string.
string = "ABC"
reversed_string = string[::-1]
print("Original string:", string)
print("Reversed string:", reversed_string)
Output:
Original string: ABC
Reversed string: CBA