This post shows how to reverse a string in Python — one of the most commonly asked basic programming questions, with several different ways to do it.
Reverse a string in Python:
Explanation:
Python strings support slice notation, so text[::-1] reverses a string in a single step: it means "take the whole string, stepping backward by 1." This is the simplest approach, though it's worth also knowing how to do it with a loop for interviews.
Python Program to Reverse a String
text = input("Enter a string: ")
reversed_text = text[::-1]
print(f"Reversed string: {reversed_text}")
Output:
Enter a string: python
Reversed string: nohtyp
Interviewers often ask you to reverse a string without slicing — try implementing it with a loop yourself in our Code Runner.
