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 withjoin()- 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.
Method 1: Slice Notation (Recommended)
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.
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 nohtyPFor 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.
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.
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 ollehIn 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:
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")) # TrueReversing 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 HelloSee Modify Strings for more string transformation techniques.
Which Method Should You Use?
| Method | Readable | Fast | Memory | Use when… |
|---|---|---|---|---|
[::-1] | Yes | Fastest | Low | Almost always — this is the Python idiom |
reversed() + join() | Yes | Fast | Low | You want to make intent explicit |
| For loop | Verbose | Slow (O n²) | Low | Learning or when you need to filter/transform while reversing |
list.reverse() | Yes | Moderate | Higher | You already have a list and want in-place mutation |
| Recursion | Yes | Slow | Stack | Academic / 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.