Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
‘ECNALUBMA’ Were you able to read it? Try reading it out from the right side, and then you will hit the right word. That was nothing but just an example of string reverse in python that many of you must be familiar with. This article will learn how we can perform string reverse in python.
Reversing a string has many utilities and is essential for preparing for interviews. We generally have a built-in function to reverse a string in other programming languages, but the same does not apply to python. So we will learn various methods using different concepts to reverse a string in python.
Let us start with the first method using a loop to perform string reverse in python.
This can be termed one of the easiest way in which we can perform string reverse in python.
First, define a string reverse function in python and create a variable that stores the reversed string. Now a variable ‘i’ iterates through the string and stores its value in the newly declared variable to reverse the string.
Let us look at its code to understand the code better.
Code
#String reverse in python using loops
#defining a function reverse and passing a variable ‘c’
def reverse(c):
string = ""
#initiating the loop
for i in c:
string = i + string
return string
#Taking the string as input
c = "AMBULANCE"
print ("String before reversing is : ",end="")
print (c)
print ("String after reversing is : ",end="")
print (reverse(c))
You can also try this code with Online Python Compiler
Let us now look at the other methods to know how we can execute string reverse in python.
Using recursion
You all must know that recursion is nothing but a simple iteration of functions to achieve a particular result. It sounds easy, but it can give you the real pain of coding while solving complex problems. We can perform string reverse in python with the help of recursion.
We will define a string reverse function in python at the beginning of the code and check whether the string is empty. If yes, we return the string as we cannot reverse an empty string. If the string is not null, we move to the next part, which separates the string's first letter and concatenates it in the end. This function gets a recursive call until the string is empty, and at the end of all recursive calls, the resultant string will be reversed.
Let us look at its code to get a clear understanding of one of the easiest applications of recursion.
Code
#String reverse in python using recursion
#defining a function
def reverse(c):
#Checking whether the string is empty
if len(c) == 0:
return c
else:
#Concating the first element at the last of the string
return reverse(c[1:]) + c[0]
c = "RECURSION"
print ("String before reversing is : ",end="")
print (c)
print ("String after reversing is : ",end="")
#Printing the desired result
print (reverse(c))
You can also try this code with Online Python Compiler
The stack data structure works on the Last in first out (LIFO) technique, which eventually will reverse a string if we push the string and pop it character-wise. This is again an elementary method to perform string reverse in python.
Let us look at its code to understand this method clearly.
Code
#String reverse in python using stack
#Creating a stack using the createStack function
def createStack():
stack=[]
return stack
#Determining the size of stack
def size(stack):
return len(stack)
def isEmpty(stack):
#Checking whether the stack is empty
if size(stack) == 0:
return true
#Function which pushes an element into the stack
def push(stack,item):
stack.append(item)
#Function to pop an element from the stack
def pop(stack):
if isEmpty(stack): return
return stack.pop()
#Function which reverses the string using stack
def reverse(str):
n = len(str)
stack = createStack()
for i in range(0,n,1):
# Push all characters of string to stack
push(stack,str[i])
str=""
for i in range(0,n,1):
#Pop all characters of string from the stack
str+=pop(stack)
return str
c = "STACK"
print ("String before reversing is : ",end="")
print (c)
print ("String after reversing is : ",end="")
print (reverse(c))
You can also try this code with Online Python Compiler
It is one of the most commonly used techniques to reverse a string in python.
Let us look at its code first, and then we will try to explain the gist behind the code.
Code
#String reverse in python using slicing
#Defining a function
def reverse(str):
#Creating an extended slice
str = str[::-1]
return str
#Taking the string as input
c = "SLICING"
print ("String before reversing is : ",end="")
print (c)
print ("String after reversing is : ",end="")
#Producing the desired output
print (reverse(c))
You can also try this code with Online Python Compiler
In the above code, we created a function that passes a variable ‘str’ as a parameter, and in the next line we create an extended slice, Extended slice allows you to enter a "step" field as [start, stop step], and leaving it blank means that start and stop default to 0 and string length, respectively, and "-1" means that the string starts at the end and ends at the beginning, reversing the string.
The above code will produce the following output.
Output
String before reversing is : SLICING
String after reversing is : GNICILS
>
After successfully learning four different methods by which we can perform string reverse in python, we will end this blog here. If you want to code in different languages, check out the following articles.
What are the ways to perform string reverse in python? The different ways to perform string reverse in python are as follows: Using loop Using recursion Using slicing Using stack
Is there any inbuilt string reverse function in python? No, there is no inbuilt string reverse function in python.
How to reverse a string in python in the easiest way ? All ways discussed above are easy, but still, using a loop can be termed as the easiest of all.
Are the string immutable in python? Yes, the string is immutable in python, which means We cannot update their value.
How to reverse a string in C++? We have numerous ways to reverse a string in c++, including the built-in functions. You can read Reverse a string in C++ for a more detailed understanding. Let us now summarize our learnings in the next section.
Key takeaways
In this article, we have extensively discussed how to reverse a string in python and its implementation in python. We discussed a total of four different methods beginning with using a loop. We saw how to reverse a string using recursion. We followed it by using stack, and in the end, we discussed the slicing technique.
We hope that this blog has helped you enhance your knowledge regarding string reverse in python and if you would like to learn more, check out our articles on Code studio. Do upvote our blog to help other ninjas grow.