Python Program to Reverse String

Write a Python program to Reverse a String using For Loop, while loop, and Functions with an example. In this programming language, there is no built-in string method to reverse the characters.

Python String Reverse using slicing

If we use the negative value inside the slice, it reads string characters from last to first.

txt = "New York"

rev = txt[::-1]

print("Original = ", txt)
print("Reversed =", rev)
Python String Reverse using slicing

Python Program to Reverse a String using For Loop

This program allows the user to enter any sentence. Next, this code reverses the string using For Loop.

st1 = input("Please enter your own : ")

st2 = ''

for i in st1:
    st2 = i + st2
    
print("\nOriginal = ", st1)
print("After = ", st2)
Python Program to Reverse a String using For Loop

You can observe from the above program screenshot that the word is Coding.

For Loop First Iteration: for i in st1
=> for C in Coding
str2 = C + st2=> C + ”

Second Iteration: for o in Coding
st2 = o + C => oC

Do the same for the remaining iterations. Please refer to the String article to understand them in Python.

Using for loop range function

An alternative approach uses the for loop range function with the start value as the last character, stop as the first char, and step as the negative one to read from last to first.

txt = "New York"

rev = ""

for i in range(len(txt) - 1, -1, -1):
    rev += txt[i]

print(rev)
kroY weN

Using for loop index

It reads characters based on the index position and reverses the items.

txt = "New York City"

rev = ""

for i in range(len(txt)):
    rev = txt[i] + rev

print(rev)
ytiC kroY weN

Python Program to reverse a String using While Loop

This program using the while loop is the same as above. However, we just replaced the For Loop with the While Loop. Here, the len function is used to find the length of the total characters in a sentence.

# using a while loop
a = input("Please enter your own : ")

b = ''
i = len(a) - 1

while(i >= 0):
    b = b + a[i]
    i = i - 1
    
print("\nThe Original = ", a)
print("The Inverted = ", b)
Python Program to reverse a String using While Loop

Python Program to Reverse String using Function

It is another way of reversing a string using functions.

def StrInverse(str1):
    str2 = str1[::-1]
    return str2

a = input("Please enter your own : ")

b = StrInverse(a)
print("\nOriginal = ", a)
print("After = ", b)
Python Program to Reverse String using Function

Using slice join function

txt = "New Jersey"

rev = "".join(txt[::-1])

print(rev)
yesreJ weN

Using join and reversed function

This example uses the join and reversed functions to reverse a given string.

txt = "New Jersey"

rev = "".join(reversed(txt))

print(rev)
yesreJ weN

import collections library deque function

from collections import deque

txt = "New York City Hall"
dq = deque(txt)
dq.reverse()

rev = "".join(dq)

print(rev)
llaH ytiC kroY weN

Python Program to Reverse String using Recursion

We are doing string reverse in this code by calling the function recursively.

# using recursive function
def StrInverted(str1):
    if(len(str1) == 0):
        return str1
    else:
        return StrInverted(str1[1:]) + str1[0]

str2 = input("Please enter your own : ")

str3 = StrInverted(str2)
print("\nThe Original = ", str2)
print("The Inversed = ", str3)
Python Program to Reverse String 4