Because the Python string library does not include an in-built “reverse()” function like other Python containers like list, understanding additional techniques to reverse a string might be beneficial. Several approaches are discussed in this article.
What is the definition of a string?
We’ll go through the basic operations that may be performed on a string, as well as the many alternatives available. We’ll also look at some examples to help us better grasp these python functions. Before we begin, it’s important to realise that strings are stored as arrays in computer languages. As a result, we can always use strings to do all array and list operations. Slicing, copying, reversing, and a variety of other operations are only a few examples. Strings are arrays that contain data of the character type.
How to reverse a string in python?
When learning a language like C, we are given built-in functions that do the reverse function. Python, on the other hand, does not have any built-in functions for this. The array slicing procedure is required.
-
Making use of loops
Program
# Python code to reverse a string
# using loop
def reverse(s):
str = “”
for i in s:
str = i + str
return str
s = “mango”
print (“The original string is : “,end=””)
print (s)
print (“The reversed string(using loops) is : “,end=””)
print (reverse(s))
Output:
The original string is : mango
The reversed string(using loops) is : ognam
Explanation: In the above code, we call a function to reverse a string, which iterates over each element and smartly joins each character at the start to produce the reversed string.
-
Using recursion
Program
# Python code to reverse a string
# using recursion
def reverse(s):
if len(s) == 0:
return s
else:
return reverse(s[1:]) + s[0]
s = “mango”
print (“The original string is : “,end=””)
print (s)
print (“The reversed string(using recursion) is : “,end=””)
print (reverse(s))
Output:
The original string is : mangoThe reversed string(using recursion) is : ognamExplanation: The string is supplied as an argument to a recursive function in the preceding code to reverse the string. The base condition of the function is that the string is returned if the length of the string is equal to 0. If the value is not 0, the reverse function is used recursively to slice the string except for the first character and concatenate the first character to the end of the sliced string.
-
Using stack
Program
# Python code to reverse a string
# using stack
# Function to create an empty stack. It
# initializes size of stack as 0
def createStack():
stack=[]
return stack
# Function to determine the size of the stack
def size(stack):
return len(stack)
# Stack is empty if the size is 0
def isEmpty(stack):
if size(stack) == 0:
return true
# Function to add an item to stack . It
# increases size by 1
def push(stack,item):
stack.append(item)
# Function to remove an item from stack.
# It decreases size by 1
def pop(stack):
if isEmpty(stack): return
return stack.pop()
# A stack based function to reverse a string
def reverse(string):
n = len(string)
# Create a empty stack
stack = createStack()
# Push all characters of string to stack
for i in range(0,n,1):
push(stack,string[i])
# Making the string empty since all
# characters are saved in stack
string=””
# Pop all characters of string and put
# them back to string
for i in range(0,n,1):
string+=pop(stack)
return string
# Driver code
s = “mango”
print (“The original string is : “,end=””)
print (s)
print (“The reversed string(using stack) is : “,end=””)
print (reverse(s))
Output:
The original string is : mangoThe reversed string(using stack) is : ognam Explanation: This command creates an empty stack. String characters are pushed to the top of the stack one by one. All characters from the stack are popped one by one and returned to the string.
-
Using reversed
Program
# Python code to reverse a string
# using reversed()
# Function to reverse a string
def reverse(string):
string = “”.join(reversed(string))
return string
s = “mango”
print (“The original string is : “,end=””)
print (s)
print (“The reversed string(using reversed) is : “,end=””)
print (reverse(s))
Output:
The original string is : mangoThe reversed string(using reversed) is : ognamExplanation: The reversed() method provides a reversed iterator of the supplied text, which is then merged with an empty string using join (). As a result, a reversed order string is created.
Conclusion
Python is without a doubt the most string-friendly programming language available. Python allows you a tremendous amount of manipulation. Consider a few key functions from the Python string module.
There are various helpful constants and classes in the string module, as well as deprecated legacy functions that are also available as methods on strings. To generate formatted strings, utilise template strings or the percent operator. Re, which stands for regular expressions, is another Python package.
Frequently Asked Questions:-
For More Information:-Python
Read More:-How to Remove Watermark From PDF By 2 Easy Methods