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.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Print Variable

Python Print Variable

Table of Contents Hide How to use the print() function in Python?How to Print variable in Python?Method 1: Using comma , character to separate the variables in a print statementMethod…
View Post
Indentationerror Unexpected Indent

IndentationError: unexpected indent

Table of Contents Hide What are the reasons for IndentationError: unexpected indent?Python and PEP 8 Guidelines Solving IndentationError: expected an indented blockExample 1 – Indenting inside a functionExample 2 – Indentation…
View Post
Python String Rindex()

Python String rindex()

Table of Contents Hide rindex() Syntaxrindex() Parametersrindex() Return ValueDifference between rindex() method and rfind() methodExample 1: Find the last occurence of a string in PythonExample 2: If string is not…
View Post