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.
  • Using slice notation
  • Using for loop
  • Using while loop
  • Using recursion
  • 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]
    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
    The post How to Reverse a String in Python appeared first on ItsMyCode.

    23

    This website collects cookies to deliver better user experience

    How to Reverse a String in Python