Last updated on April 24th, 2025 at 07:23 am
Often Python developers are required to demonstrate how to reverse string in Python. This is a common interview question in software development and IT companies. There are several ways to reverse string in Python. In this article, we will learn how to do this.
Why Reverse String in Python
You might be wondering why someone would need to reverse a string in Python. There are several use cases for this problem. First of all, it is used to check if a string is a palindrome or not. A palindrome is a string that remains the same even when the sequence of its characters is reversed. Next, it is used to decrypt/encrypt data that might have been stored or transmitted in reverse order. You can also use it to manipulate texts and data. For example, it can be a part of a bigger algorithm such as the need to create a mirror image of text in an image. As you can see, there are plenty of useful applications of string reversal so it is good to know how to do it.
How to Reverse String in Python
Here are the different ways to reverse string in Python.
1. Using Slicing
One of the most common and fastest ways to do string reversal is to use slicing. Slicing is generally used to extract a substring or a range of characters from a string, list or tuple. However, it can also be used to reverse a string. Here is its syntax.
string[start:end:step]
- start – index to begin slicing. It is included in the slice. It is optional and defaults to 0.
- end – index to end slicing. It is excluded from the slice. It is optional and defaults to the string length.
- step – increment between indices in the slice. It is also optional and defaults to 1. A negative step value will reverse the slice.
Using this method, we can reverse string with the following command.
string[::-1]
In the above command, we have kept the start index and end index empty. So it will use the entire string. We have used -1 value so that the string is traversed in reverse from the end to the start. The result is reversed string. Here is an example to demonstrate it.
data = 'hello world'
print(data[::-1]) # dlrow olleh
print(data) # hello world
Please note, in the above example, the original string data remains unchanged, since Python strings are immutable. So if you want to modify the original string, you need to re-assign the result of slicing back to the string variable data.
data = 'hello world'
data=data[::-1]
print(data) # dlrow olleh
Alternatively, you can save the result of slicing as another string variable
2. Using reversed()
If you want to reverse a large body of text then using the above method may consume a lot of space. In such cases, you can use reversed() function that basically returns an iterator to the string characters in reverse order. This iterator does not occupy much memory and is fast. You can generate the actual reversed string by calling join() function on this iterator.
data = 'hello world'
print(''.join(reversed(data))) # dlrow olleh
print(data) # hello world
In the above code, we use join() function along with empty string ” so that all characters returned from reversed() function are concatenated to form a single string.
3. Using Loops
If you want to have more control over string reversal process, then you can use a for loop for this purpose. In this case, we create an empty string to store reversed string. Then we simply loop through the string and prepend each character to the reversed string.
data = 'hello world'
data2= ''
for i in data:
data2=i+data2
print(data2) # dlrow olleh
This approach allows you to customize the string reversal as per your requirements. Here is an example where we exclude character ‘e’ from reversed string.
data = 'hello world'
data2= ''
for i in data:
if i!='e':
data2=i+data2
print(data2) # dlrow ollh
4. Using List Comprehension
If you are an advanced Python programmer, and want a concise piece of code that also gives you good control over string reversal, then you can use list comprehension along with join() function.
data = 'hello world'
data2=''.join([data[i] for i in range(len(data) - 1, -1, -1)])
print(data2) # dlrow olleh
In the above article, we use range() function to generate a range of numbers starting from len(data)-1, that is, 10, and going down to -1 (excluded), with a step of -1. It results in a range from 10 to 0. We use this range’s items as index values, to access the string characters in reverse order. In our list comprehension we loop through this range, from 10 to 0, and use these numbers as indexes to get string variables in reverse order. Since the result of list comprehension is a list, we call join() function on this list to convert it into a string.
Please note: There are also other solutions that use stack, or recursion, but we have avoided them since they are memory intensive and can cause your system to crash in case of large strings, or code bugs.
Conclusion
In this article, we have learnt several different ways to easily reverse a string in Python. Generally, string reversal programs are parts of larger modules and algorithms. So it is good to know how to reverse Python string. Slicing is the most convenient and easiest solution to reverse string. If you want more control over string reversal, then you can use simple for loops and customize them as per your requirement. If you need to reverse a really large text, then you can use reversed() function along with join() function, since reversed() returns a memory-efficient iterator which is also fast.
Also read:
How to Compare Strings in Python
How to Remove Characters from String in Python
What Are f-strings in Python? How to Use f-strings?

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.