Reverse a String in Python Without using Loop

We will develop a program to reverse a string in python without using loop. 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.

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

Reverse a String Without using Loop in Python

We will take a string while declaring the variables. Then, find the reverse of string using a predefined function join(reversed()). Python reversed() method returns an iterator that accesses the given sequence in the reverse order. Finally, the result will be displayed on the screen.

# Python program to reverse a string without using loop

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

Enter the string: functions
The reverse string is snoitcnuf

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 Loop

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 without using loop

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: slicing
The reverse string is gnicils

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 Loop

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.

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.

# Python program to reverse a string without using loop

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

Enter the string: recursion
The reverse string is noisrucer

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

Enter the string: Python
The reverse string is nohtyP

Leave a Comment

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