Reverse a String in Python
Reverse a string entered by the user.
BeginnerString ProgramsExample 1 of 25
reverse-a-string.py
Run in browser1# Program to reverse a string23s = input("Enter a string: ")45reversed_s = s[::-1]67print("Reversed string:", reversed_s)
Output
Enter a string: hello Reversed string: olleh
What's going on
We use Python's slicing syntax s[::-1] to create a reversed copy of the string.