Python Program to Reverse a String Using Recursion

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Recursion in Python:

Python also supports function recursion, which means that a specified function can call itself.

Recursion is a mathematical and programming concept that is widely used. It signifies that a function calls itself. This has the advantage of allowing you to loop through data to obtain a result.

The developer must exercise extreme caution when using recursion since it is quite easy to write a function that never terminates or consumes excessive amounts of memory or computing power. However, when performed correctly, recursion may be a tremendously efficient and mathematically elegant way to programming.

Examples:

Example1:

Input:

given string = btechgeeks

Output:

The modified given string{after reversing} = skeeghcetb

Example2:

Input:

The original given string = aplustopper

Output:

The modified given string{after reversing} = reppotsulpa

Program to Reverse a String Using Recursion in Python

Below are the ways to reverse a given string using recursion in Python.

1)Using Recursion(Static Input)

Approach:

  • Give the string from the user as static input and store it in a variable.
  • We can also use recursion to reverse a string. The process through which a function calls itself in Python is known as recursion.
  • Calculate the length of the given string using the len() function.
  • To begin, we declared the reverseRecursion() function and gave given_string as an input if                len(given_string ) == 1 is used to check the length of the string, which is the fundamental condition of recursion. If the length of the string is 1, the string is returned, otherwise, the function is called recursively.
  • The slice operator will slice the string and concatenate it to the end of the slice string if it anticipates the first character.
  • Finally, it will return the reverse order and print the reversed string.
  • The exit of the program.

Below is the implementation:

# function which accepts the given string as an argument and
# reverse the given string using recursion and return the reversed string


def reverseRecursion(given_string):
   # Calculate the length of the given string using the len() function.
    stringLen = len(given_string)
    # if len(str1) == 1 is used to check the length of the string, which is the fundamental condition of recursion. If                  the length of the string is 1,
    # the string is returned, otherwise, the function is called recursively.
    if stringLen == 1:
        return given_string
    else:
      # The slice operator will slice the string and concatenate it to the end of the
      # slice string if it anticipates the first character.
        return reverseRecursion(given_string[1:]) + given_string[0]


# Give the string from the user as static input and store it in a variable.
givenstring = 'btechgeeks'
# printing the original given string
print('The original given string =', givenstring)
# passing the given string as an argument to the recursive function
# 'reverseRecursion' which reverses the given string.

print('The modified given string{after reversing} = ',
      reverseRecursion(givenstring))

Output:

The original given string = btechgeeks
The modified given string{after reversing} =  skeeghcetb

2)Using Recursion(User Input)

Approach:

  • Scan the given string from the user as user input using the int(input()) function.
  • We can also use recursion to reverse a string. The process through which a function calls itself in Python is known as recursion.
  • Calculate the length of the given string using the len() function.
  • To begin, we declared the reverseRecursion() function and gave given_string as an input if              len(given_string ) == 1 is used to check the length of the string, which is the fundamental condition of recursion. If the length of the string is 1, the string is returned, otherwise, the function is called recursively.
  • The slice operator will slice the string and concatenate it to the end of the slice string if it anticipates the first character.
  • Finally, it will return the reverse order and print the reversed string.
  • The exit of the program.

Below is the implementation:

# function which accepts the given string as an argument and
# reverse the given string using recursion and return the reversed string


def reverseRecursion(given_string):
   # Calculate the length of the given string using the len() function.
    stringLen = len(given_string)
    # if len(str1) == 1 is used to check the length of the string, which is the fundamental condition of recursion. If the length of the string is 1,
    # the string is returned, otherwise, the function is called recursively.
    if stringLen == 1:
        return given_string
    else:
      # The slice operator will slice the string and concatenate it to the end of the
      # slice string if it anticipates the first character.
        return reverseRecursion(given_string[1:]) + given_string[0]


# Scan the given string from the user as user input using the int(input()) function.able.
givenstring = input('Enter some random string = ')
# printing the original given string
print('The original given string =', givenstring)
# passing the given string as an argument to the recursive function
# 'reverseRecursion' which reverses the given string.

print('The modified given string{after reversing} = ',
      reverseRecursion(givenstring))

Output:

Enter some random string = aplustopper
The original given string = aplustopper
The modified given string{after reversing} = reppotsulpa

Related Programs: