Reverse a String in Python

Reverse a string entered by the user.

BeginnerString ProgramsExample 1 of 25
reverse-a-string.py
1# Program to reverse a string
2
3s = input("Enter a string: ")
4
5reversed_s = s[::-1]
6
7print("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.

After this program

If the idea is still fuzzy, read Day 4: Strings and String Methods in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 string programs, or see every topic on Python exercises.