How to Reverse a String in Python (3 Easy Methods)

How to Reverse a String in Python (3 Easy Methods)

Often in Python, you may want to reverse a string. There are several ways to achieve this, and in this tutorial, we’ll explore three different ways to reverse a string in Python and compare their performance.

Method 1: Using a Loop

The most basic method is to manually reverse a string by iterating through it with a for loop. For Example:

text = "All men are born equal"
reversed_string = ""

for char in text:
    reversed_string = char + reversed_string

print("reversed =", reversed_string)

Output:

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 powerful slicing feature, [: : -1], to reverse a string in one line.

text = "All men are born equal"
reversed_string = text[: : -1]
print("reversed =", reversed_string)

Output:

reversed = lauqe nrob era nem llA

Here, [: : -1] means:

  • Start at the end of the string.
  • Move backwards one step at a time (a step of -1).
  • 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.

text = "All men are born equal"
reversed_string = " ".join(reversed(text))
print("reversed =", reversed_string)

output

reversed = lauqe nrob era nem llA

This approach is explicit, readable, and works well when you want to emphasize the use of built-in functions.

Comparing the Performance of all three methods

For small strings, all methods work fine. But when reversing large strings (millions of characters), slicing is much faster because it’s optimized in C inside Python.

Let’s compare the methods by measuring execution times using timeit:

import timeit

text = "All men are born equal" * 1000000  # very long string

# Loop method
loop_time = timeit.timeit(
    'reversed_string = " ".join([c for c in text[: : -1]])',
    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)

Sample Output (times vary by computer):

Loop method time: 4.82 seconds
Slicing method time: 0.03 seconds
reversed() + join time: 0.05 seconds

Conclusion:

For real-world projects, using slicing ([: : -1]) is the recommended approach due to its superior efficiency (when you want speed and simplicity) and readability.

Additional Resources

If you enjoyed this tutorial, check out these related Python guides: