How do I reverse a string in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python strings are immutable sequences, so reversing a string always creates a new string. The most common and idiomatic way is slicing with [::-1], but there are several other approaches depending on your needs — including reversed(), manual loops, and recursion.
Method 1: Slicing (Recommended)
The slice [::-1] steps through the string backwards, producing a reversed copy:
The slice syntax is [start:stop:step]. Omitting start and stop means "the entire string," and step=-1 means "go backwards."
Method 2: reversed() + join()
The reversed() built-in returns an iterator over the string in reverse order. Use "".join() to build the result:
This is slightly more readable than slicing for developers unfamiliar with the [::-1] idiom, but it is slower because it creates an iterator and then joins.
Method 3: Loop Accumulation
Build the reversed string character by character:
This is O(n²) because string concatenation creates a new string each time. Use a list for better performance:
Or more efficiently with append and a final reverse:
Method 4: Recursion
A recursive approach splits the string and reassembles in reverse:
This hits Python's recursion limit (~1000) for long strings and is very slow due to repeated string concatenation. It is mainly useful as an exercise.
Method 5: reduce()
A functional approach using functools.reduce:
Performance Comparison
| Method | Time Complexity | Speed | Readability |
s[::-1] | O(n) | Fastest | Pythonic |
"".join(reversed(s)) | O(n) | Fast | Clear intent |
list(s).reverse() + join | O(n) | Fast | Verbose |
| Loop prepend | O(n²) | Slow | Simple |
| Recursion | O(n²) | Slowest | Educational |
Reversing Words (Not Characters)
To reverse word order while keeping each word intact:
Unicode Considerations
Slicing works correctly with most Unicode, but combined characters (like accented letters using combining marks) can break:
For safe Unicode reversal, use the grapheme library or unicodedata normalization.
Common Pitfalls
- Strings are immutable: All methods create a new string. You cannot reverse a string in place in Python.
- O(n²) concatenation: Building a string with
+=or prepend in a loop is quadratic. Uselist+joininstead. - Recursion limit:
reverse_string()via recursion fails on strings longer than ~1000 characters. Usesys.setrecursionlimit()cautiously or avoid recursion altogether. - Unicode combining characters:
[::-1]reverses codepoints, not grapheme clusters. Combined emoji or accented characters may break.
Summary
- Use
s[::-1]for the fastest, most Pythonic string reversal - Use
"".join(reversed(s))for explicit readability - Avoid loop-based or recursive approaches in production code due to poor performance
- To reverse word order, split first then reverse:
" ".join(s.split()[::-1]) - Be cautious with Unicode combining characters when reversing
Related reading
- How do I reverse a string in Python?
- How do I run a Python script from C?
- How do I run a Python script from C?
- How do I run all Python unit tests in a directory?
- How do I run celery status/flower without the -A option?
- How do I run Python code from Sublime Text 2?
- How do I sample a line across a blob at a perpendicular angle? in Python/OpenCV unless you suggest switching to something else
- How do I select certain columns of a 2D tensor in TensorFlow?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.