How to Reverse a String in Python
text[::-1] returns a new string with the characters in reverse order. "".join(reversed(text)) does the same thing and may read more clearly to beginners.Python strings have no reverse() method, because strings are immutable and cannot be changed in place. Instead you build a new string that contains the same characters backwards. Reversing text is useful for palindrome checks, for reading data that was stored back to front, and as a classic practice exercise.
The fastest and most common way is a slice with a step of -1. The other ways below are slower or longer, but they show what is really happening and are handy when you need to reverse something other than a plain string.
| Shortest answer | text[::-1] |
|---|---|
| Changes the original? | No. Strings are immutable, so you get a new string |
| Works on | Any sequence: strings, lists, tuples |
| Time complexity | O(n) for every method shown here |
Reverse a string with slicing [::-1]
A slice has the form text[start:stop:step]. Leaving start and stop empty means "the whole string", and a step of -1 means "walk backwards one character at a time". The result is a reversed copy.
word = "python"
print(word[::-1])
print(word)The original word is untouched. Slicing is written in C inside Python, so it is the quickest option. See Python slicing for how start, stop and step work.
Reverse with reversed() and join()
The built-in reversed() returns an iterator that yields the characters from last to first. It does not return a string, so you glue the characters back together with str.join().
word = "python"
print("".join(reversed(word)))If you forget join() and print reversed(word) directly, you only see something like <reversed object at 0x...>.
Reverse a string with a loop or recursion
A loop that prepends each character shows the idea step by step. It is much slower on long strings because each + builds a new string, so use it for learning, not production.
word = "python"
result = ""
for ch in word:
result = ch + result
print(result)A recursive version (reverse(s[1:]) + s[0]) also works, but Python limits recursion depth, so it fails on strings of roughly a thousand characters or more.
Which way to reverse a string should you choose?
| Method | Code | Best for |
|---|---|---|
| Slicing | s[::-1] | Everyday use; fastest |
| reversed() + join() | "".join(reversed(s)) | Readability; works on any iterable |
| Loop | for ch in s: out = ch + out | Learning how reversal works |
| Recursion | rev(s[1:]) + s[0] | Recursion practice only |
Reverse the words in a sentence instead of the characters
Sometimes "reverse a string" really means reversing the order of the words. Split the text into a list of words, reverse that list, and join it back with spaces. Splitting is explained in how to split a string into words.
Examples you can run
Press Run, then edit the code and run it again.
Reverse a string with slicing
A step of -1 walks through the string from the end to the start.
name = "Asha"
reversed_name = name[::-1]
print(reversed_name)
print(name)Expected outputahsA
Asha
Reverse with reversed() and join()
reversed() gives characters one by one; join() turns them back into a string.
message = "hello world"
print("".join(reversed(message)))Expected outputdlrow olleh
Check whether a word is a palindrome
A palindrome reads the same in both directions, so compare the text with its reverse. Lowercasing first ignores capital letters.
def is_palindrome(text):
cleaned = text.lower()
return cleaned == cleaned[::-1]
print(is_palindrome("Level"))
print(is_palindrome("Python"))Expected outputTrue
False
Reverse the order of the words
Split, reverse the list, then join with a space. The words themselves are not reversed.
sentence = "learn python every day"
words = sentence.split()
print(" ".join(words[::-1]))Expected outputday every python learn
Edge cases: empty string and unicode
An empty string reverses to an empty string. Slicing works on Unicode code points, so plain accented letters are fine.
print(repr(""[::-1]))
print("caf\u00e9"[::-1])Expected output''
éfac
Common mistakes
Calling a reverse() method on a string
✗ Wrong
text = "python"
text.reverse()✓ Right
text = "python"
text = text[::-1]
print(text)Only lists have reverse(). For strings, slice with [::-1] and keep the returned value.
Printing reversed() without join()
✗ Wrong
print(reversed("abc"))✓ Right
print("".join(reversed("abc")))reversed() returns an iterator, not a string. Wrap it in "".join(...).
Expecting the original string to change
✗ Wrong
word = "stressed"
word[::-1]
print(word)✓ Right
word = "stressed"
word = word[::-1]
print(word)Slicing returns a new string. Assign it back to a variable if you want to keep it.
How to Reverse a String in Python: questions people ask
How do you reverse a string in Python without using slicing?
Use "".join(reversed(text)) or build the result in a loop with result = ch + result. Both avoid slicing and give the same output.
Is there a reverse() function for strings in Python?
No. Strings have no reverse() method; only lists do. Use text[::-1] or reversed() with join() instead.
How do I check if a string is a palindrome in Python?
Compare the string with its reverse: text == text[::-1]. Lowercase it first with text.lower() if capital letters should be ignored.
Keep learning
Step-by-step lessons:
Python StringsPython SlicingPython String Methods
Related:
Python String join() MethodHow to Split a String into Words in PythonPython List reverse() MethodHow to Capitalize the First Letter of a String in Python
More in working with text:
How to Check if a String Contains a Substring in PythonHow to Remove Whitespace from a String in PythonHow to Split a String into Words in PythonHow to Compare Strings Ignoring Case in PythonHow to Pad a Number with Leading Zeros in PythonHow to Capitalize the First Letter of a String in Python See all →