Top Ways to Reverse String in Python

Hello everybody, During this tutorial we’ll see alternative ways to reverse a string in Python.

As we know, we are able to reverse an inventory victimisation reverse() technique however Python doesn’t have the reverse() method for the string.

Here are some alternate and simple ways to reverse a string.

Ways to Reverse String in Python

Using Loop

string1 =  "comman man"
string2 = ""
 
i = len(string1)-1
 
while(i>=0):
  string2 = string2 + string1[i]
  i = i-1
 
print "original = " + string1
print "reverse  = " + string2

OutPut

original = common man
reverse = remmargorp yzarc eht

In on top of program, we’ve started a loop from the last index (length-1) to initial index (0) of string1. In every step of loop, it’ll decide the character from right-side in string1 and concatenate with string2.

2. Using Recursion

def reverse_it(string):
  if len(string)==0:
    return string
  else:
    return reverse_it(string[1:]) + string[0]
    print "added " + string[0]
 
string1 = "comman man"
string2 = reverse_it(string1)
 
print "original = " + string1
print "reversed = " + string2

In on top of the program, there’s a reverse_it() technique that accepts a string then it’ll check if the string is empty or not, if empty then it’ll come back the string otherwise it will decide itself by passing string from its second character to last character.

String = “hello”

Print string[1:]

Output: ‘ello’

After job reverse_it() technique once more and again there’ll be a degree once the string is going to be empty then the condition

if len(string) == 0:

will be true, it’ll come back the string. come back statement can throw the execution, wherever it came from.

So in

Return reverse_it(string[1:]) + string[0]

the “+ string[0] “is going to be dead next, which is able to add the primary letter ultimately.

3.Using Stack

def create_stack():
  #it will  create a List named as stack and return it
  stack = []
  return stack
 
def push(stack,element):
  #it will add a new element to List
  stack.append(element)
 
def pop(stack):
  #it will delete the last element from  List
  if len(stack) == 0:
    return
  return stack.pop()
 
def reverse(string):
 
  #method to reverse the string using stack's functions
  n = len(string)
  
  #to create a empty list (stack)
  stack = create_stack()
 
  #inserting character of string into List
  for i in range(0,n):
    push(stack,string[i])
 
  #making string empty
  string = ""
 
  #getting last element of the List (stack) and storing it into string
  for i in range(0,n):
    string = string + pop(stack)
  return string
 
string1 = "comman man"
string2 = reverse(string1)
 
print "original = " + string1
print "reversed = " + string2

In on top of the program, we’re victimisation construct of the stack having push and pop functions.

To implement stack construct we’re victimisation list.

When we decision reverse() technique, it’ll produce an inventory named as ‘stack’ and insert all the characters of string into list victimisation push() technique.

ultimately it’ll fetch all the weather within the list from last to initial one by one and store them into the string.

4.Using Extended Slice

string = "comman man"
print "original = " + string
 
string = string[::-1]
print "reversed = " + string

Mostly extended slice is employed for skipping the steps however if we have a tendency to place -1 in third ‘step’ or ‘stride’ argument then we are able to get the reverse of a string, list and tupple.

5.Using List

string = "comman man"
print "original = " + string
 
#convrting string into list
list1 = list(string)
 
#applying reverse method of list
list1.reverse()
 
#converting list into string
string = ''.join(list1)
print "reversed = " + string

The string doesn’t have reverse() technique however lists have. therefore we have a tendency to are changing a string into a list, performing arts reverse() operation and once more changing it back to string victimisation ‘ ’.join() technique.

Comment below if you’ve got queries or apprehend the other to reverse a string in python.

Leave a Comment

error: Alert: Content is protected!!