Reverse String in Python

Learn how to reverse a string in Python using slicing, reversed() with join(), for loop, and while loop. Understand why string[::-1] is the simplest method and avoid common mistakes.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

Reverse String in Python

Reversing a string means creating a new string where the characters appear in the opposite order. Python strings are immutable, so every reversal method returns a new string instead of changing the original.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


Quick answer: reverse a string in Python

Use text[::-1].

python
text = "hello"
print(text[::-1])
Output

This prints olleh. For most scripts, slicing is the simplest and most common way to reverse a string. Use "".join(reversed(text)) when you want to work with the reversed() iterator, but slicing is the default choice.


Python reverse string quick reference

Task Use
Reverse a string text[::-1]
Reverse using a function def reverse_string(text): return text[::-1]
Reverse with reversed() "".join(reversed(text))
Reverse using for loop Build result by adding characters in front
Reverse using while loop Read characters from last index to first
Reverse words, not characters " ".join(text.split()[::-1])
Check palindrome text == text[::-1]
Avoid text.reverse() because strings do not have reverse()

Reverse string using slicing

text[::-1] is the recommended default. In slice notation text[start:stop:step], a step of -1 walks backward through the string and returns a new string.

python
text = "Python"
print(text[::-1])
print(text)
Output

The first print is nohtyP. The original text is unchanged.

Python sequence slicing supports s[i:j:k], where k is the step. For a negative step, omitted start and stop values use the appropriate end of the sequence. See Python substring and slicing for more slice patterns.


Reverse string using a function

Wrap slicing in a small function when you want a reusable helper.

python
def reverse_string(text):
    return text[::-1]

print(reverse_string("hello world"))
Output

This prints dlrow olleh. Keep the function simple and use reverse_string naming instead of names like reverseFunction.


Reverse string using reversed() and join()

reversed(text) returns a reverse iterator, not a string.

python
text = "hello"

print(reversed(text))
print("".join(reversed(text)))
Output

The first line shows a reversed object. The second line prints olleh.

The Python docs define reversed() as returning a reverse iterator over a sequence.


string[::-1] vs reversed() with join()

Method Returns Best for
text[::-1] New string Simple string reversal
"".join(reversed(text)) New string Teaching iterators or working with generic reversible sequences
reversed(text) Reverse iterator Looping over characters backward

Reverse string using for loop

A for loop builds the reversed string by prepending each character. This is useful for learning, not the best default in production.

python
text = "hello"
reversed_text = ""

for char in text:
    reversed_text = char + reversed_text

print(reversed_text)
Output

This prints olleh.

For large strings, repeated concatenation can be slow because strings are immutable. The Python docs note that building strings from many small pieces is better done with a list and join().

python
text = "hello"
chars = []

for char in text:
    chars.insert(0, char)

print("".join(chars))
Output

Reverse string using while loop

A while loop reads characters from the last index down to zero.

python
text = "hello"
reversed_text = ""
index = len(text) - 1

while index >= 0:
    reversed_text += text[index]
    index -= 1

print(reversed_text)
Output

This prints olleh. Use this mainly for index-based learning; prefer text[::-1] in real code.


Reverse words in a string

Reversing characters and reversing word order are different tasks.

Character reverse:

python
text = "hello world"
print(text[::-1])
Output

This prints dlrow olleh.

Word order reverse:

python
text = "hello world"
print(" ".join(text.split()[::-1]))
Output

This prints world hello. The query “reverse string order” sometimes means word order, not character order. See Python split string for more ways to break text into words.


Reverse string and check palindrome

A palindrome reads the same forward and backward.

python
def is_palindrome(text):
    return text == text[::-1]

print(is_palindrome("level"))
print(is_palindrome("hello"))
Output

This prints True, then False.

If spaces or letter case should be ignored, normalize first:

python
def is_palindrome(text):
    cleaned = text.lower().replace(" ", "")
    return cleaned == cleaned[::-1]

print(is_palindrome("Never odd or even"))
Output

This prints True for the normalized phrase.


Can you reverse a string in place?

No. Python strings are immutable sequences of Unicode code points, so you cannot swap characters inside the original string object.

You can convert to a list, swap characters, and join back, but that is usually unnecessary when text[::-1] already solves the task.

python
text = "hello"
chars = list(text)
chars.reverse()
print("".join(chars))
print(text)
Output

This prints olleh, then the unchanged original hello.


Common mistakes with reversing strings

  • Calling text.reverse() on a string
  • Thinking reversed(text) returns a string
  • Forgetting "".join() with reversed()
  • Confusing reverse characters with reverse word order
  • Using list.reverse() and expecting a string result without joining
  • Using str as a variable name
  • Using expensive repeated concatenation on very large strings
  • Naming helpers reverseFunction instead of reverse_string
  • Forgetting strings are immutable
  • Expecting perfect visual reversal for every Unicode combining mark or emoji
  • Filling a string article with repeated list-reversal examples

Summary

Use text[::-1] for most string reversal in Python. Use "".join(reversed(text)) when you want to demonstrate reversed(). For loops and while loops are mainly for learning how reversal works. Strings are immutable, so reversal always produces a new string. To compare a string with its reverse for palindrome checks, see Python compare strings.


References


Frequently Asked Questions

1. What is the easiest way to reverse a string in Python?

Use text[::-1]. It returns a new string with characters in reverse order and is the most common approach for everyday string reversal.

2. Does reversed() reverse a string directly in Python?

No. reversed(text) returns a reverse iterator. Use "".join(reversed(text)) when you need the final reversed string.

3. Can you reverse a string in place in Python?

No. Python strings are immutable. Reversal always creates a new string, unless you convert to a mutable sequence first.

4. How do you reverse word order instead of characters?

Use " ".join(text.split()[::-1]) to reverse the order of words while keeping each word unchanged.

5. How do you check if a string is a palindrome in Python?

Compare the string to its reverse with text == text[::-1]. Normalize case or spaces first if your rules require it.

6. Why does text.reverse() fail on strings?

The reverse() method exists for mutable lists, not strings. Strings do not have reverse().
Bashir Alam

Data Analyst and Machine Learning Engineer

Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in OCR, text extraction, data preprocessing, and …