Updated March 23, 2023
Introduction to Python Reverse String
The following article provides an outline for Python Reverse String. Often, we come across a situation where we are required to do manipulation on words or text in our program to get the end result. Words or text in the program are known as strings which are nothing but a sequence of character data. The datatype of Strings in Python is known as str and is defined by any set of characters declared within single or double-quotes.
Example:
Code:
text = "A quick brown fox jumps over the lazy dog."
The following variable text is of type str, and it contains the value “A quick brown fox jumps over the lazy dog.”. Now many actions can be done on strings depending on the requirement. One such action is reversing a string. Our writings are left-aligned, i.e. we write from left to right of the margin. When we reverse a string, the result is the right to the original string’s left representation.
Let us give an example to elaborate on this:
- Original String: “The reverse string in Python is very cool.”
- Reversed String: “. looc yrev si nohtyP ni gnirts esrever ehT.”
It is so funny that a simple reversal of a text can be so confusing at times. But this has many applications in programming, and we must know all the ways of reversing a text in Python.
Examples of Python Reverse String
Python, as we know, is the most widely used programming language in today’s world, so it is a must to include it in your programming arsenal. Here, we would look at the different ways with which we can reverse a text in Python and do a comparative study of them at last.
Example #1: Using For Loops
A string can be thought of as a list of characters, so for reversing a string, we can iterate through it using a for loop and then append the characters to a new variable from back to front. Let us have a look at the code for that.
Code:
from datetime import datetime
text = "The reverse string in Python is very cool."
reversed_text = ""
time_start = datetime.now().microsecond
for character in text:
reversed_text = character + reversed_text
time_finish = datetime.now().microsecond
print("Original text :",text)
print("Reversed text :",reversed_text)
print("Time taken to reverse string using for loop :",str(time_finish-time_start)," microseconds")
Output:
Example #2: Using While Loops
Instead of using For Loops, we can also use while loops for getting the reverse string. Let us have a look at the code for that.
Code:
from datetime import datetime
text = "The reverse string in Python is very cool."
reversed_text = ""
length = len(text) - 1
time_start = datetime.now().microsecond
while(length>=0):
reversed_text = reversed_text + text[length]
length = length - 1
time_finish = datetime.now().microsecond
print("Original text :",text)
print("Reversed text :",reversed_text)
print("Time taken to reverse string using for loop :",str(time_finish-time_start)," microseconds")
Output:
Example #3: Using Recursion
Recursion is a method of programming in which a function calls itself multiple times unless a condition is met.
Code:
from datetime import datetime
text = "The reverse string in Python is very cool."
def reverse_recursion(text):
if len(text) == 0:
return text
else:
return reverse_recursion(text[1:]) + text[0]
time_start = datetime.now().microsecond
reverse_recursion(text)
time_finish = datetime.now().microsecond
print("Original text :",text)
print("Reversed text :",reverse_recursion(text))
print("Time taken to reverse string using for loop :",str(time_finish-time_start)," microseconds")
Output:
Example #4: Using List Reverse
A string is a list of characters, so it can be reversed using the reverse() function of the list.
Code:
from datetime import datetime
text = "The reverse string in Python is very cool."
def reverse_list(text):
text_list = list(text)
text_list.reverse()
return ''.join(text_list)
time_start = datetime.now().microsecond
reverse_list(text)
time_finish = datetime.now().microsecond
print("Original text :",text)
print("Reversed text :",reverse_list(text))
print("Time taken to reverse string using for loop :",str(time_finish-time_start)," microseconds")
Output:
Example #5: Using join() and reversed() Function
Code:
from datetime import datetime
text = "The reverse string in Python is very cool."
def reverse_join_reversed(text):
reversed_text = ''.join(reversed(text))
return reversed_text
time_start = datetime.now().microsecond
reverse_join_reversed(text)
time_finish = datetime.now().microsecond
print("Original text :",text)
print("Reversed text :",reverse_join_reversed(text))
print("Time taken to reverse string using for loop :",str(time_finish-time_start)," microseconds")
Output:
Example #6: Using Slicing
Code:
from datetime import datetime
text = "The reverse string in Python is very cool."
time_start = datetime.now().microsecond
print("Original text :",text)
print("Reversed text :",text[::-1])
time_finish = datetime.now().microsecond
print("Time taken to reverse string using for loop :",str(time_finish-time_start)," microseconds")
Output:
From the above examples, it is evident that there are many ways to reverse a string in Python. But we must always follow the method which is most concise and fast.
Conclusion
In this article, we saw the concept of reversing a string in Python and looked at the various methods of achieving that, along with their syntax. We also did a comparative study and found out that the slicing operation is the best option for reversing a string due to its fast computational time. Now that we have studied everything about reversing a string, it is time to put it to practice.
Recommended Articles
This is a guide to Python Reverse String. Here we discuss the introduction and examples of python reverse string for better understanding. You can also go through our suggested articles to learn more –