How to reverse a string in Python?
A complete guide to reversing a string data type in Python
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.
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:
- Appending characters in reverse order using a loop
- Using the built-in
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.
We loop through the characters in the
original_string and append them in the reverse order in the
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 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.
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.
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 ..
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:
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!