Python Reverse String

String reverse in python; Through this tutorial, i am going to show you how to reverse string using function, for loop, recursion and without function in python.

There are many way to reverse a string in python. In this tutorial, i will show you simple 5 ways to reverse string in python.

How to Reverse String in Python

Use the following 5 methods to reverse a string in Python; As shown in the list below:

  • 1: Reverse string in python using for loop
  • 2: Python reverse string using slicing
  • 3: Python reverse string using recursion
  • 4: Reverse a string using while loop in python
  • 5: Reverse a string using reversed() function in python

1: Reverse string in python using for loop

See the following python program to reverse a string using for loop; as shown below:

# Python program to reverse a string  
# using for loop
def reverse(s): 
  str = "" 
  for i in s: 
    str = i + str
  return str
  
str = "Python World"
  
print ("Given string  is : ",end="") 
print (str) 
  
print ("Reversed string using loops is : ",end="") 
print (reverse(str)) 

Output

Given string  is : Python World 
Reversed string using loops is : dlroW nohtyP 

2: Python reverse string using slicing

See the following python program to reverse a string using slicing:

# Python program to reverse a string  
# using slice 
  
# Function to reverse a string 
def reverse(string): 
    string = string[::-1] 
    return string 
  
str = "Python World"
  
print ("Given string  is : ",end="") 
print (str) 
  
print ("Reversed string using extended slice is : ",end="") 
print (reverse(str)) 

Output

Given string  is : Python World 
Reversed string using extended slice is : dlroW nohtyP 

3: Python reverse string using recursion

See the following python program to reverse a string using recursion:

# Python program to reverse a string  
# using recursion 
  
def reverse(s): 
    if len(s) == 0: 
        return s 
    else: 
        return reverse(s[1:]) + s[0] 
  
str = "Python World"
  
print ("Given string  is : ",end="") 
print (str) 
  
print ("Reversed string using recursion is : ",end="") 
print (reverse(str)) 

Output

Given string  is : Python World 
Reversed string using recursion is : dlroW nohtyP 

4: Reverse a string using while loop in python

See the following python program to reverse a string using while loop:

# Python program to reverse a string  
# using while loop 
  
def reverse_string(input):
    string = ""
    length = len(input) - 1
    while length >= 0:
        string = string + input[length]
        length = length - 1
    return string
str_data = 'My world'
print ("Given string  is :" + str_data) 
if __name__ == "__main__":
    print('Reverse String using While Loop =', reverse_string(str_data))

Output

Given string  is :My world 
Reverse String using While Loop = dlrow yM 

5: Reverse a string using reversed() function in python

See the following python program to reverse a string using while loop:

def rev_str_thru_join_revd(STR):
    return "".join(reversed(STR))	

INPUT_STRING = "My world" 

if __name__ == '__main__':

    print("INPUT STRING -", INPUT_STRING)
    print("RESERVED STRING THROUGH JOIN & REVERSED", rev_str_thru_join_revd(INPUT_STRING))

Output

Given string  is :My world 
Reverse String using While Loop = dlrow yM 

Recommended Python Tutorials

Recommended:-Python Lists
Recommended:-Python Strings

Leave a Comment