How to reverse a string in Python?

A complete guide to reversing a string data type in Python

Chaitanya Baweja
Sep 18, 2019 · 3 min read
Photo by Devin Avery on Unsplash

Python is a no-bullshit general-purpose programming language with an army of built-in libraries. Using polymorphism, we can use standard functions with a wide variety of data structures.

This is why it’s extremely strange that the Python String Library doesn’t support the built-in reverse() function.

So, I decided to write an article discussing several ways of reversing a string in Python. I will also try to judge each of these methods based on execution speed and code clarity.

Some common ways to reverse a string are as follows:

  1. Slicing
  2. Appending characters in reverse order using a loop
  3. Using reversed() iterator
  4. Using the built-in reverse() function
  5. Recursion

Reversing a list using Slicing

Python Slicing allows us to extract a part of a sequence. This is a very direct way of reversing a list without affecting the original list.

original_string = "ABCDE"
reversed_string = original_string[::-1]
print("Original String : ", original_string)
print("Reversed String : ", reversed_string)
# Output
# Original String : ABCDE
# Reversed String : EDCBA

Explanation : Slice has the following fields [start,stop,step]. In original_string[::-1], we keep start and stop to default values (0 and string length). This means that the entire string will be copied.

Keeping the step size to -1 means starting from the end and stopping at the start. This causes string reversal.

If you are new to Python and slicing, I would recommend going through this post.

Appending characters in reverse order using a loop

Again a pretty straightforward technique using only a few lines of code.

Code for reversing string by appending characters in reverse order using a loop

We loop through the characters in the original_string and append them in the reverse order in the reversed_string.

Using the reversed iterator

The reversed() function returns the reversed iterator of the given string. We then stitch the elements together in a string using the join() operation.

Code for reversing string using the reversed() iterator

It is important to note that this method works for a variety of sequences (strings, lists, range, tuples).

This might be useful when we are not sure about the type of input data, but we need the output as a reversed string.

Using the built-in reverse function

The built-in reverse() function cannot be used directly with the string data type. This is why we first convert the string into a temporary list. We then reverse the list and use the join() function to stitch it into a string.

Code for reversing string using the reverse() function

Using Recursion

While not as intuitive as other methods, but recursion can also be used for reversing a string.

In each step, we take out the first element of the string using slicing. The base case is when we have a single element remaining. Once the base case is reached, the elements are stitched back together in reverse order.

Code for reversing string using recursion

Step by step process for better understanding of how the algorithm works.

Let the string be “ABCDE”.

Step 1-> (send “BCDE” to next step) + “A”

Step 2-> (send “CDE” to next step) + “B”

Step 3-> (send “DE” to next step) + “C”

Step 4-> (send “E” to next step) + “D”

Step 5-> Return “E” as string length == 1

Step 6-> Return “ED” and so on ..

Return “EDCBA”

Comparing the Different Methods

We saw five ways in which we can reverse a string. But which one of them should you pick?

The most readable one is Slicing. Those new to Python might find the loop method more intuitive. reversed() works for all sorts of data types.

Now, let’s use the timeit module to check the average time required to run each of these methods.

I executed all these functions one by one for 1,00,000 times and took the average out. The average run-time in microseconds for the above-listed methods is as follows:

Table comparing execution speed of various methods for reversing a string in Python

We see that Slicing is by far the fastest method.

I would recommend using slicing to reverse a string in Python. Its code is simple and concise. Also, it’s the fastest way to reverse a string as identified by the above test executions.

You can find links to my other work on Medium and follow me here. Thanks for reading!

Additional Further Reading

  1. GeeksforGeeks: Python String Reverse
  2. Indexing and Slicing
  3. Python reversed

The Startup

Medium's largest active publication, followed by +567K people. Follow to join our community.

Chaitanya Baweja

Written by

Machine Learning Engineer | Python | Data Analytics | Economics | Physics

The Startup

Medium's largest active publication, followed by +567K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade