Reverse a String in Python using Function

We will develop a program to reverse a string in python using function. We can also take the help of a function to reverse a string in python using different methods. A function is a block of code that performs a specific task.

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

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

Function to Reverse a String in Python

In this program, we use the for loop to reverse a string. The for loop iterates every element of the given string, joining each character in the beginning so as to obtain the reversed string.

# 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 = 'Know Program'

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

Output:-

The reverse string is margorP wonK

In the previous program, inputs are hardcoded in the program but in this program, input will be provided by the user.

# 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: Python
The reverse string is nohtyP

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

Enter the string: reverse
The reverse string is esrever

Reverse a String in Python using Function

We are using the while loop to reverse a string. We initialized a while loop with a value of the string.

# Python program to reverse a string using function

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:-

Enter the string: function
The reverse string is noitcnuf

Reverse a String in Python using Recursion

We can also 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

Reverse a String in Python using Slicing

We read a string and reverse a string using slice operations. Syntax of slicing operation:- string[::-1]

# 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: slice
The reverse string is ecils

Leave a Comment

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