We all know that python uses the Unicode format to store data. A string is an array of bytes in which string is stored. It means that the string is nothing but the array of characters. To reverse a string, we can use an array index, or there are many ways to reverse a string in python. Let’s understand one by one.
Table of Contents
How to reverse a string in python?
There are six ways to reverse a string.
1. Using for loop
2. Using while loop
3. Using the Stack data structure
4. Using reverse() function
5. Using recursion
6. Using slice operator
These all are the ways to reverse a string. You can reverse it using any one of them. Besides, the fastest way to reverse a string is by using the slice operator. Slice operator takes less time compared to others. Let’s understand these ways one by one in detail with the help of examples.
Reverse a string using for loop
Reversing a string using a for loop is very easy. Declare one empty string and store the original string’s last index as the reversed string’s first index. Let’s understand this with an example.
Example
str = str(input("Enter a string: ")) #Accept string from user
print("The original string is: ", str)
rev = ""
for i in str: # for loop for reversing a string
rev = i + rev
print("The reversed string is: ",rev)
Output
Enter a string: HelloWorld
The original string is: HelloWorld
The reversed string is: dlrowolleh
Also, we can create a user-defined reverse function in python.
Example
def rev(str): #Declaring a user defined function
rev = ""
for i in str:
rev = i + rev
return rev
str = str(input("Enter a string: "))
print("The original string is: ", str)
print("The reverse string is", rev(str)) #Function call
Output
Enter a string: helloworld
The original string is: helloworld
The reverse string is dlrowolleh
Reverse a string using while loop
Using a while loop for reversing a string is quite confusing. We need to maintain a counter for the iterations. Otherwise, the loop will iterate infinite times.
Example
str = str(input("Enter a string: ")) #Accept string from user
print("The original string is: ", str)
rev =""
i = len(str) - 1 #counter
while(i >= 0): # while loop for reversing a string
rev = rev + str[i]
i = i - 1
print("The reversed string is: ",rev)
Output
Enter a string: Ilovepython
The original string is: Ilovepython
The reversed string is: nohtypevolI
Reverse string in python using Stack
We all know that a stack data structure follows the LIFO manner that is last in the first out manner. The Stack is also used to reverse a string. Let’s see how to use it.
Example
str = str(input("Enter a string: "))
print("The original string is ",str)
stack = []
rev = [] #empty stack to store data in reverse order
for i in str:
stack.append(i)
while(len(stack)):
x = stack.pop()
rev.append(x)
for i in rev:
print(i, end="")
Output
Enter a string: i love python
The original string is i love python
nohtyp evol i
Using an inbuilt reverse() function to reverse a string
Using the reverse function to reverse a string is the simplest way. Pass the string as a parameter to the reverse function. It will return the object of the reverse of string in python. The example is given below.
Example
str = str(input("Enter a string: "))
print("The original string is ",str)
rev = reversed(str) #call to the reversed function
print(rev)
output
Enter a string: hello
The original string is hello
<reversed object at 0x000001F01FAF0DC8>
But we need a string. We use join() to print a string, which will join the string’s object to the empty string and help print the expected output.
Example
str = str(input("Enter a string: "))
print("Original string is ",str)
rev ="".join(reversed(str)) #call to the reversed function
print(rev)
Output
Enter a string: This is my first program
Original string is This is my first program
margorp tsrif ym si sihT
Using recursion to reverse words in a string python
We need the slice operator to reverse a string in python. Making a recursive call to the function will return the string in reverse order. Let’s understand it with an example.
Example
def rev(str):
if len(str) == 0:
return str
else:
return rev(str[1:]) + str[0] # recursive call to the function
str = str(input("Enter a string: "))
print("The original string is: ",str)
print("The reversed string using recursion is: ",rev(str));
Output
Enter a string: Hello world
The original string is: Hello world
The reversed string using recursion is: dlrow olleH
Using slice operator to get string reverse in python
Using a slice operator is the best way to reverse a string. This will take less time compared to other ways of reversing a string. Let’s see how to use the slice operator in python to reverse a string.
Example
str = str(input("Enter a string: "))
print(str[::-1])
Output
Enter a string: Hello python
nohtyp olleH
We can also create a function to call it multiple times in a program.
Example
def rev(str): #user defined function
return str[::-1] #this will return the string in reverse order
str = str(input("Enter a string: "))
reverse = rev(str) #Call to the function
print("The reversed string is: ",reverse)
Output
Enter a string: i love programming
The reversed string is: gnimmargorp evol i