How to Reverse a String in Python

Python has many functions for string manipulation. However, the Python string library does not have any built-in reverse() function to reverse a string.

Reverse a string in Python

Let us look at the various other approaches to reverse a string in Python. Below are some of the approaches which can reverse the Python string.

  1. Using slice notation
  2. Using for loop
  3. Using while loop
  4. Using recursion
  5. Using reversed() method

Using extended slice operator

The easiest and fastest way to reverse a string in Python is using slice notation. The extended slice syntax is as follows.

string[start:stop:step]

The slice starts at the end of the string and traverses backwards with a step of -1 till it reaches position 0.

# Python reverse a string using extended slice operator

def reverse(str):   
    str = str[::-1]   
    return str  

text= "ItsMyCode"
print("The reversed string is :", reverse(text))

Output

The reversed string is : edoCyMstI

Using for loop

The naive way to reverse a string is using the for loop. Each element in the string will be iterated using the for loop, and then it appends the characters to the beginning of a new string to obtain a reversed string.

# Python reverse a string using for loop

def reverse(s):
    str = ""
    for i in s:
        str = i + str
    return str


text = "ItsMyCode"
print("The reversed string is :", reverse(text))

Output

The reversed string is : edoCyMstI

Using while loop

Like for loop, we can also use a while loop to reverse a string. We will determine the length of the string inside the while loop. We will join the characters from the end of the string and decrementing the count till it reaches 0.

# Python reverse a string using while loop

def reverse(s):
    str = ""
    count = len(s)
    while count > 0:
        str += s[count - 1] 
        count = count - 1  # decrement index
    return str


text = "ItsMyCode"
print("The reversed string is :", reverse(text))

Output

The reversed string is : edoCyMstI

Using recursion

Recursion is a process where the function calls itself. In the code below, we will call a recursion method by passing a string as an argument to reverse the string. 

# Python reverse a string using recursion

def reverse(s):
    if len(s) == 0:
        return s
    else:
        return reverse(s[1:]) + s[0]

text = "ItsMyCode"
print("The reversed string is :", reverse(text))

Output

The reversed string is : edoCyMstI

Using reversed() method

The reversed() returns the reversed iterator of the given string, and then its elements are joined empty string separated using join(). This approach will be the slowest approach when compared to the slicing approach.

# Python reverse a string using reversed

def reverse(str):
    str = "".join(reversed(str))
    return str

text = "ItsMyCode"
print("The reversed string is :", reverse(text))

Output

The reversed string is : edoCyMstI
1 comment
Leave a Reply

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

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy.average() Function

numpy.average() Function

Table of Contents Hide SyntaxParametersReturn ValueRaisesExample 1: NumPy average() of Array values Example 2: NumPy average() of an array values in column-wise Example 3: NumPy average() of an array values in row-wiseExample…
View Post