Reverse a String in Python Without using inbuilt Function

We will develop a program to reverse a string in python without using inbuilt function. We read a string and reverse a string using slice operations.

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

Reverse a String Without using inbuilt Function in Python

Syntax of slicing operation:- string[::-1]

# Reverse a string in Python without using inbuilt function

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 for the input values test-case-1:-

Enter the string: slice
The reverse string is ecils

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

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

Reverse a String in Python Without using inbuilt Function

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.

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.

# Reverse a string in Python without using inbuilt 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

Python Program to Reverse a String Without using inbuilt Function

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

# Reverse a string in Python without using inbuilt 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

String Reverse in Python Without using inbuilt Function

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 handle easily. This is also a well-known computer programming technique: divide and conquer.

# Reverse a string in Python without using inbuilt function

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

Leave a Comment

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