Python Program to Reverse a String

In this post, we will learn how to reverse a string using Python Programming language.

Reversing a string can be defined as an operation in which the original string which the user has entered is modified in such a way that characters in it arrange in reverse order, starting from the last character to the first character.

We will be reversing a string using following methods:

  1. Using For Loop
  2. Using Functions
  3. Using While Loop

So, without further ado, let’s begin this tutorial.

Python Program to Reverse a String

# Python Program to Reverse a String
str = input("Enter a string: ")

# Defining reverse string
revstr = ""

for i in str:
    revstr = i + revstr

# Displaying output 
print("Original String: ", str)
print("Reverse String: ", revstr)

Output

Enter a string: Hello
Original String:  Hello
Reverse String:  olleH

How Does This Program Work ?

str = input("Enter a string: ")

The user is asked to enter a string which he wants to reverse.

# Defining reverse string
revstr = ""

Then, we declare a variable named revstr which will store the reverse of a string.

for i in str:
    revstr = i + revstr
  • We reverse the string using for loop. 
  • After first iteration, revstr = i + revstr = “H” + “” = H
  • After second iteration, revstr = i + revstr = “e” + “H” = eH
  • After third iteration, revstr = i + revstr = “l” + “eH” = leH
  • After four iteration, revstr = i + revstr = “l” + leH = lleH
  • And after fifth and last iteration, revstr = i + revstr = “o” + “lleH” = olleH
# Displaying output 
print("Original String: ", str)
print("Reverse String: ", revstr)

Finally, the reversed string is displayed on the screen using print() function.

Python Program to Reverse a String Using Functions

# Python Program to Reverse a String Using Functions
def Reverse(str):
    revstr = str[::-1]
    return revstr
    
# Asking for input
str = input("Enter a string: ")
result = Reverse(str)

# Displaying output
print("Original String: ", str)
print("Reversed String: ", result)

Output

Enter a string: Amazing
Original String:  Amazing
Reversed String:  gnizamA

Python Program to Reverse a String Using While Loop

# Python Program to Reverse a String Using While Loop
str = input("Enter a string: ")

revstr = ""
i = len(str) - 1

# Reversing the string
while (i >= 0):
    revstr = revstr + str[i]
    i = i - 1
    
# Displaying output
print("Original String: ", str)
print("Reversed String: ", revstr)

Output

Enter a string: CodingBroz
Original String:  CodingBroz
Reversed String:  zorBgnidoC

Conclusion

I hope after going through this post, you understand how to reverse a string using Python Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.

Also Read:

1 thought on “Python Program to Reverse a String”

  1. Carlos Herrera

    As a Python developer, I can say that it is a powerful language where we can code less and get output in very little time and with fewer efforts. Most of the students asked me about the question mentioned as to how to reverse a string using Python language. The article provides excellent three ways to solve this problem, i.e., using In-built Function, using while loop, and using for loop. I generally prefer the In-built function because it requires less time, and you will learn about the remaining in-built functions.

    It is needless to say that you can refer to this website to get in-depth knowledge of the python program and various methods in python. Thank you for providing such helpful information to us.

Leave a Comment

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