Python Program to Reverse a String

We will develop a Python program to reverse a string. In this article, we are using the for loop, while loop, reversed() function, slice operator, and recursion methods to reverse a string in python.

Example of reverse string:-
String: Know Program
Reverse String: margorP wonK

Reverse a String in Python using For Loop

We will take a string while declaring the variables. Then, the for loop iterates every element of the given string, joining each character in the beginning so as to obtain the reversed string. Finally, the result will be displayed on the screen.

The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string. The range() method returns an immutable sequence of numbers between the given start integer to the stop integer.

Program description:- Write a python program to reverse a string from user input

# Python program to reverse a string using for loop

# take inputs
string = input('Enter the string: ')

# calculate reverse of string
reverse = ''
for i in range(len(string), 0, -1):
   reverse += string[i-1]

# print reverse of string
print('The reverse string is', reverse)

Output for the input values test-case-1:-

Enter the string: Know Program
The reverse string is margorP wonK

Output for the input values test-case-2:-

Enter the string: reverse
The reverse string is esrever

Output for the input values test-case-3:-

Enter the string: For loop
The reverse string is pool roF

Function to Reverse a String in Python

We can also take the help of a function to reverse a string in python. A function is a block of code that performs a specific task.

# Python program to reverse a string using function

def findReverse(string):  #user-defined function
   # find reverse of string
   reverse = ''
   for i in range(len(string), 0, -1):
      reverse += string[i-1]
   return reverse

# take inputs
string = input('Enter the string: ')

# calling function and display result
reverse = findReverse(string)
print('The reverse string is', reverse)

Output for the input values test-case-1:-

Enter the string: function
The reverse string is noitcnuf

Output for the input values test-case-2:-

Enter the string: Python
The reverse string is nohtyP

Program to Reverse a String in Python using While Loop

In the previous program, we used the for loop but in this program, we use the while loop to reverse a string. We initialized a while loop with a value of the string.

Program description:- Write a program to reverse a string in Python

# Python program to reverse a string using while loop

def findReverse(string):  #user-defined function
   #find reverse of string
   i = string
   reverse = ''
   while(len(i) > 0):
      if(len(i) > 0):
         a = i[-1]
         i = i[:-1]
         reverse += a
   return reverse

# take inputs
string = input('Enter the string: ')

# calling function and display result
reverse = findReverse(string)
print('The reverse string is', reverse)

Output for the input values test-case-1:-

Enter the string: while loop
The reverse string is pool elihw

Output for the input values test-case-2:-

Enter the string: reverse of string
The reverse string is gnirts fo esrever

Python Program to Reverse a String using Slicing

The slice() function returns a slice object. A slice object is used to specify how to slice a sequence. You can specify where to start the slicing, and where to end.

# Python program to reverse a string using slicing

def findReverse(string):  #user-defined function
   # find reverse of string
   reverse = string[::-1]
   return reverse

# take inputs
string = input('Enter the string: ')

# calling function and display result
reverse = findReverse(string)
print('The reverse string is', reverse)

Output:-

Enter the string: slicing
The reverse string is gnicils

We are using a predefined function join(reversed()). Python reversed() method returns an iterator that accesses the given sequence in the reverse order.

Program description:- Write a python function to reverse a string

# Python program to reverse a string using inbuilt function

def reverse(string):   #user-defined functon
   # find reverse using buit-in functions
   reverse = ''.join(reversed(string))
   return reverse

# take inputs
string = input('Enter the string: ')

# calling function and display result
print('The reverse string is', reverse(string))

Output:-

Enter the string: inbuilt function
The reverse string is noitcnuf tliubni

Reverse String Recursion in Python

We can use the recursion technique to reverse a string in Python. A technique of defining the method/function that contains a call to itself is called recursion. The recursive function/method allows us to divide the complex problem into identical single simple cases that can be handled easily. This is also a well-known computer programming technique: divide and conquer.

# Python program to reverse a string using recursion

def findReverse(string):  #user-defined function
   # find reverse of string
   if len(string) == 0:
      return string
   else:
      return findReverse(string[1:]) + string[0]

# take inputs
string = input('Enter the string: ')

# calling function and display result
reverse = findReverse(string)
print('The reverse string is', reverse)

Output:-

Enter the string: recursion
The reverse string is noisrucer


Q1) Write a python program to reverse a string sample string 1234abcd.

# Python program to reverse a string

def findReverse(string):  #user-defined function
   # find reverse of string
   reverse = ''
   for i in range(len(string), 0, -1):
      reverse += string[i-1]
   return reverse

# take inputs
string = '1234abcd'
# calling function and display result
print('The reverse string is', findReverse(string))

Output:-

The reverse string is dcba4321

Leave a Comment

Your email address will not be published. Required fields are marked *