5 Ways to Reverse String in Python
Hello everyone, Here we will see different ways to reverse string in Python.
In list sequence of python , we can reverse a list using reverse() method but Python doesn’t have the reverse() method for string.
Here are some alternate and easy ways to reverse a string.
Ways to Reverse String in Python
Method:1
Using for loop
#reverse a string using for loop
string1 = “rajeshshuklacatalyst”
string2 = “”
i = len(string1)-1
while(i>=0):
string2 = string2 + string1[i]
i = i-1
print(“original = ” , string1)
#or
#print(“original = ” + string1)
print(“reverse = ” , string2)
#or
#print(“reverse = ” + string2)
output:
original = rajeshshuklacatalyst
reverse = tsylatacalkuhshsejar
Method : 2
Using Recursion
def reverse_string(string):
if len(string)==0:
return string
else:
#print(“added ” + string[0])
return reverse_it(string[1:]) + string[0]
string1 = “rajeshshuklacatalyst”
string2 = reverse_string(string1)
print(“original = ” + string1)
print(“reversed = ” + string2)
output:
original = rajeshshuklacatalyst
reversed = tsylatacalkuhshsejar
In above program,
* there is a reverse_string() method which accepts a string.
* then it will check if the string is empty or not,
* if empty then it will return the string otherwise it will call itself by passing string from its second character to last character.
String = “hello”
Print string[1:]
Output: ‘ello’
* After calling reverse_string() method again and again there will be a point when string will be empty then the condition
if len(string) == 0:
will be true , it will return the string.
* on execution of the statment
Return reverse_it(string[1:]) + string[0]
“+ string[0]” it will add the first letter at last.
Method :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(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 = “rajeshshuklacatalyst”
string2 = reverse_string(string1)
print(“original = ” + string1)
print(“reversed = ” + string2)
In above program, we are using the concept of stack which has two important operations push and pop functions.
Here to implement stack concept we are using list.
When we call reverse_string() method, it will create a list named as ‘stack’ and insert all the characters of string into list using push() method.
At last it will fetch all the elements in the list from last to first one by one and store them into the string.
Method : 4
Using extended Slice
string = “rajesh shukla catalyst”
print(“original = ” + string)
string = string[::-1]
print(“reversed = ” + string)
output:
original = rajesh shukla catalyst
reversed = tsylatac alkuhs hsejar
Method : 5
Using List
string = “rajesh shukla catalyst”
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)
output:
original = rajesh shukla catalyst
reversed = tsylatac alkuhs hsejar
String does not have reverse() method but lists have.
So we are converting string into list, performing reverse() operation and again converting it back into string using ‘ ’.join() method.