In this article, we would like to show you how to reverse a string in Python.
reversed_string = string[::-1]
Where the statement
- start at the end of the string,
- end at position 0,
- move with the step
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)
Original string: ABC Reversed string: CBA