4 Python Programs to reverse a string

PythonBasic Python

In this post, we are going to explore different 4 Python Programs to reverse a string. that includes reverse string using recursion, reverse string using for loop, reverse string using slicing.

1. Python program to reverse string using for loop


In this Python program we have defined a function that takes string that we have to reversed as an argument inside in function we are iterating over all character of string using for loop and concatenating character reversing order.

  • After iteration is finished returning the reversed string.
  • Asking user to input the string to reverse.
  • calling the function reverse_str() by passing the input string and printing the result.
def reverse_str(Str):
  resStr = ""
  for char in Str:
    resStr = char + resStr
  return resStr

input_Str = input("Enter a string to reversed:")  

  

print (reverse_str(input_Str))

Output

Enter a string to reversed:devenum
muneved

2. Python Programs to reversed string


In this example we have defined a function reverse_str(). Inside this function using built in function reversed() with join() and print the result using

  • Asking the user to input the string that we have to reversed.
  • Passing the enetered string to reverse_str() method to get reversed string.
def reverse_str(Str):
  resStr = "".join(reversed(Str))
  print(resStr)
  
input_Str = input("Enter a string to reversed:")  
  

reverse_str(input_Str)

Output

Enter a string to reversed:devenum
muneved

3. Python Programs to reverse string using recursion


In this Python program we have using recursion to get the reversed string we have defined a function reverse_Str() that takes string that we have reversed as an argument. If the string length is 0. We will return else calling the same function recursively.

  • Asking the user to input the string that we have to reversed.
  • Passing the enetered string to reverse_str() method to get reversed string.
def reverse_Str(Str):
  if len(Str) == 0:
      return Str
  else:
      return reverse_Str(Str[1:]) + Str[0]
      print('\n reversed string is :',reversed_Str)
  
  print(resStr)
  
input_Str = input("Enter a string to reversed:")
  

print(reverse_Str(input_Str))

Output

Enter a string to reversed:devenum
muneved

4. Python Programs to reverse string using slicing


In this Python program we have using slicing to get reversed string by defining a function reverse_Str().

  • Asking the user to input the string that we have to reversed.
  • Passing the enetered string to reverse_str() method to get reversed string.

def reverse_Str(Str):
  if len(Str) == 0:
      return Str
  else:
   resStr = Str[::-1]    
   print('\nreversed string is :',resStr)

  
input_Str = input("Enter a string to reversed:")
  

reverse_Str(input_Str)

Output

Enter a string to reversed:devenum

reversed string is : muneved

Summary

In this post, we have gone through 4 Python Programs to reverse a string that includes reverse string recursively, reverse string using slicing.