How to Reverse a String in Python

In Python, there are three main methods to reverse a string. This tutorial explains how each one works and guides you in selecting the most suitable approach for your needs.
Method 1: Using a Loop
The simplest way is to iterate over each character in the string using a for loop, adding each character to the front of a new string to build the reversed version.
1
2
3
4
5
6
7
text = "All men are born equal"
reversed_string = ""
for char in text:
reversed_string = char + reversed_string
print("reversed =", reversed_string)
Output:
1
reversed = lauqe nrob era nem llA
The final output, lauqe nrob era nem llA
, is the reversal of the original text “All men are born equal”.
The algorithm builds the reversed string by:
- Iterating over each character in the original string from left to right.
- Prepend (add to the front) each character to the
reversed_string
variable. Which effectively reverses the order of characters.
This method is great for understanding how string reversal works step by step
Method 2: Using Slicing
You can use Python’s powerful slicing feature, [: : -1],when you want the fastest, most reliable solution to reverse a string in one line. This is the method you’ll use most often in real projects
1
2
3
text = "All men are born equal"
reversed_string = text[: : -1]
print("reversed =", reversed_string)
Output:
1
reversed = lauqe nrob era nem llA
Here, [: : -1]
- slicing works by:
- [::-1] means “start at the end of the string and move backwards”.
- The
-1
tells Python to step backwards one character at a time - Continue until the beginning, effectively reversing it.
Method 3: Using reversed() and join()
You can also use the built-in reversed() function, which returns an iterator over the string in reverse order. And then joining the characters together.
1
2
3
text = "All men are born equal"
reversed_string = " ".join(reversed(text))
print("reversed =", reversed_string)
output
1
reversed = lauqe nrob era nem llA
-
reversed(text)
creates an iterator that goes through characters backwards -
"".join()
combines these characters into a new string.
Choose this method when you want clear, explicit, readable code that emphasizes Python’s built-in functions.
Performance Comparing
For small strings, all methods work fine. But when reversing large strings (processing large amounts of text,), slicing is much faster because it’s optimized in C inside Python.
Let’s test how fast each method runs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import timeit
text = "All men are born equal" * 1000000 # very long string
# Loop method
loop_time = timeit.timeit(
'reversed_string = "".join([char + "" for char in text])',
globals = globals(),
number = 10
)
# Slicing method
slice_time = timeit.timeit(
'reversed_string = text[: : -1]',
globals = globals(),
number = 10
)
# reversed() + join method
reversed_time = timeit.timeit(
'reversed_string = " ".join(reversed(text))',
globals = globals(),
number 10
)
print("Loop method time:", loop_time)
print("Slicing method time:", slice_time)
print("reversed() + join time:", reversed_time)
Typical results you’ll see (times vary by computer):
1
2
3
Loop method time: 4.82 seconds
Slicing method time: 0.03 seconds
reversed() + join time: 0.05 seconds
Conclusion
-
Use slicing (
text
[::-1]`) — it’s fast and readable -
Use
reversed() + join()
when you want explicit, clear code -
Use loops only for learning or when you need custom reversal logic.
For real-world projects, using slicing ([: : -1]) is the recommended approach due to its superior efficiency (when you want speed and simplicity) and readability.
Now you know three ways to reverse strings—try each method and see which feels most natural for your coding style.
If you enjoyed this tutorial, check out these related Python guides: