DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

How to Reverse a String in Python

ItsMyCode |

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 slicenotation. The extended slice syntax is as follows.

string[start:stop:step]
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

The reversed string is : edoCyMstI

Enter fullscreen mode Exit fullscreen mode

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))

Enter fullscreen mode Exit fullscreen mode

Output

The reversed string is : edoCyMstI

Enter fullscreen mode Exit fullscreen mode

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))

Enter fullscreen mode Exit fullscreen mode

Output

The reversed string is : edoCyMstI

Enter fullscreen mode Exit fullscreen mode

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))

Enter fullscreen mode Exit fullscreen mode

Output

The reversed string is : edoCyMstI
Enter fullscreen mode Exit fullscreen mode

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))

Enter fullscreen mode Exit fullscreen mode

Output

The reversed string is : edoCyMstI

Enter fullscreen mode Exit fullscreen mode

The post How to Reverse a String in Python appeared first on ItsMyCode.

Top comments (0)