Reversing a string in Python might be tricky sometimes. Especially when you are a new programmer, you might find reverse a string in Python difficult. There are various methods available in Python that help us to reverse a string. In this short article, we will go through the top 10 various methods to reverse a string in Python.
Reverse a String in Python
Reversing a string means simply changing the order of the string and bringing the last element to the first position and the second last to the second and so on. For example, see the diagram below which explains the concept of reversing a string in Python.

As you can see, after reversing the string, we got a new string that is the opposite of the original string. Now let us go through various methods to reverse a string in Python.
Method-1: Using Simple Print Command to reverse a string
If you are a beginner Python programmer, then you might have used the print statement a lot. The print statement is used to show something on the terminal or the console. We will use the print statement in order to show the reverse of the element.
# 1. Use a simple print command
print("This is a string"[::-1])
Output:
gnirts a si sihT
As you can see, we have changed the order of the string using the print method. Actually, the print method has been used only to show the result. The reverse has been done by slicing.
Method-2: Using Join() and Reversed() methods
The reversed() method in Python is used to reverse the object and the join() method is used to join the elements together. We can use both these methods in order to find the reverse of the string in Python.
Let us take an example and explain the code.
# creating a string
string = "This is a string"
# reverse a string in python
print("".join(reversed(string)))
Output:
gnirts a si sihT
As you can see, we got the reverse of the string.
Method-3: Use a Function, For Loop, Join(), and Reversed()
We already have used the join() and reversed() methods to reverse a string. Now, we will use these methods along with for loop inside the function which will return the reverse of the string. A Function in Python is simply a block of code that only executes when we call it to execute. The for loop is a type of loop that iterates through elements over a specified range.
Let us first go through the example to reverse the string and then we will explain the code.
# function to reverse a string in python
def reverse_string(string):
st = []
for i in string:
st.append(i)
rst = reversed(st)
return "".join(rst)
# calling the method
print(reverse_string("This is a string"))
Output:
gnirts a si sihT
Notice that the function is returning the reverse of the string.
Method-4: Using Function With Concatenation Operator
Now we will use a function that will help us to reverse a string with the help of the concatenation operator. The += operator in Python is used to add elements to the existing variable. Let us see, how we can use this += operator in Python inside a function to perform the reversing of the string.
# reverse a string in python
def reverse_string(string):
rev_str = ""
for c in reversed(string):
rev_str += c
return rev_str
# caling the method
print(reverse_string("This is a string"))
Output:
gnirts a si sihT
As shown, the string has been reversed by using a function and concatenation method.
Method-5: One-line function to reverse a string
You might have noticed, the functions we just have gone through have many lines of code. We can reduce these lines of code and can have a function with only one line that will help us to reverse a string in Python. This method will just take the first one and add it to the function as shown below:
# function to reverse a string in python
def reverse_string(string):
return string[::-1]
# caling the method
print(reverse_string("This is a string"))
Output:
gnirts a si sihT
As shown above, the code has reversed the given string.
Method-6: While Loop to reverse a string
A while loop in Python is a type of loop that iterates through the object until the condition is false. It means if the condition will not be false, it will iterate infinite time. Let us take an example and see how we can use the while loop in order to find the reverse of a string in python.
# using a while loop to reverse a string in python
def reverse_while_loop(s):
s1 = ""
l = len(s) - 1
while l >= 0:
s1 += s[l]
l -= 1
return s1
# caling the method
print(reverse_while_loop("This is a string"))
Output:
gnirts a si sihT
Method-7: Recursion Method to reverse a string in Python
Recursion in Python is simply a process through which a function calls itself until a condition is satisfied. Let us take an example of recursion and see how we can use this method in order to find the reverse of a string.
# Recursion
def reverse_recursively(s):
if len(s) == 0:
return s
else:
return reverse_recursively(s[1:]) + s[0]
# caling the method
print(reverse_recursively("This is a string"))
Output:
gnirts a si sihT
As you can see, we have found the reverse of the string using the recursion method.
Summary
In this short article, we discussed various methods through which we can reverse a string in Python. Here we discussed 7 different methods and explained each one step by step. Moreover, we have taken simple examples in order to make things clear for beginners.
Errors Related to Python Strings
- TypeError: Not Enough Arguments For Format String in Python
- TypeError: ‘str’ object does not support item assignment
- Typeerror: String Indices Must be Integers Error in Python