W3docs

Reversing Strings in Python: A Comprehensive Guide

Learn four ways to reverse a string in Python: slicing, reversed(), list.reverse(), and recursion — with runnable examples and performance tips.

Reversing a string means changing the order of its characters so the last character becomes the first, and so on. Python offers several ways to do this, each with different trade-offs in readability, performance, and memory use.

This chapter covers:

  • The idiomatic one-liner using slice notation ([::-1])
  • reversed() combined with join()
  • An in-place list reversal
  • A recursive approach (useful for learning, not for production)
  • Practical use cases: palindrome detection and reversing word order

Python strings are immutable — none of these methods modify the original string; they all return a new one.

The most idiomatic Python way to reverse a string is the extended slice [::-1]. The three-part slice [start:stop:step] defaults start and stop to the full string when omitted, and a step of -1 walks backward through every character.

python— editable, runs on the server

This works on any sequence — lists, tuples — not just strings. Because the slice is implemented in C inside CPython, it is the fastest option in practice.

sentence = "Python is great"
print(sentence[::-1])  # taerg si nohtyP

For a deeper look at start/stop/step syntax, see the Slicing Strings chapter.

Method 2: reversed() and join()

The built-in reversed() returns an iterator that yields characters from the end to the beginning. ''.join() collects those characters into a new string.

python— editable, runs on the server

When to prefer this over slicing

reversed() is more explicit about intent — it reads as "join the reversed characters of this string." Some teams prefer it in code reviews because it leaves no ambiguity about what the empty slice [::-1] means. Performance-wise, slicing is roughly 5× faster on typical string lengths because it avoids iterator overhead.

Method 3: For Loop

You can iterate over a string in reverse and build a new string character by character. This approach is verbose but makes the algorithm visible — useful in learning contexts.

python— editable, runs on the server

A note on string concatenation in a loop

Each += creates a new string object because strings are immutable. For very long strings this is slow — O(n²) time. If you need to build a reversed string one character at a time, collect characters in a list and join at the end:

string = "hello world"
chars = []
for char in reversed(string):
    chars.append(char)
reversed_string = ''.join(chars)
print(reversed_string)  # dlrow olleh

In practice, use slicing — but understanding the loop version helps when you need to apply filtering or transformation while reversing.

Method 4: list.reverse()

Convert the string to a list of characters, call the in-place .reverse() method, then join back:

string = "hello"
chars = list(string)
chars.reverse()          # mutates the list in place
reversed_string = ''.join(chars)
print(reversed_string)  # olleh
print(string)           # hello (original unchanged)

This takes more memory than the slice approach because a full list copy is created, but it is explicit and easy to read. The .reverse() method is documented in Python string methods.

Method 5: Recursion

A recursive function calls itself with a shorter version of the string until the base case (empty string) is reached:

python— editable, runs on the server

Gotcha: recursion depth limit

Python's default recursion limit is 1000 (sys.getrecursionlimit()). A string longer than ~990 characters will raise RecursionError. Use this approach only for demonstration purposes; prefer slicing or reversed() in production code.

Practical Use Cases

Checking for palindromes

A palindrome reads the same forward and backward. Reversing and comparing is the simplest check:

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

print(is_palindrome("racecar"))                    # True
print(is_palindrome("hello"))                      # False
print(is_palindrome("A man a plan a canal Panama")) # True

Reversing word order in a sentence

Reversing the whole string is different from reversing the order of words. To flip word order, split into a list and reverse that:

sentence = "Hello World Python"
reversed_words = " ".join(sentence.split()[::-1])
print(reversed_words)  # Python World Hello

See Modify Strings for more string transformation techniques.

Which Method Should You Use?

MethodReadableFastMemoryUse when…
[::-1]YesFastestLowAlmost always — this is the Python idiom
reversed() + join()YesFastLowYou want to make intent explicit
For loopVerboseSlow (O n²)LowLearning or when you need to filter/transform while reversing
list.reverse()YesModerateHigherYou already have a list and want in-place mutation
RecursionYesSlowStackAcademic / interview demonstrations only

For almost all real-world code, string[::-1] is the right choice: it is concise, fast, and immediately recognizable to any Python developer.

Practice

Practice
Which of the following correctly reverses the string 'Python'?
Which of the following correctly reverses the string 'Python'?
Was this page helpful?