How to Reverse a String in Python? [5 Ways]

Posted in /   /  

How to Reverse a String in Python? [5 Ways]
vinaykhatri

Vinay Khatri
Last updated on December 23, 2024

    Like a list reverse() method, we do not have a reverse method for Python strings. But we can use some techniques which can reverse a specified string. Here in this tutorial, we have provided some of the common techniques we can use to reverse a string in Python.

    How to Reverse a string in Python?

    1. Reverse a String Using Slicing

    String slicing is a technique which is used to select or display a sequence of characters from a string. String slicing is the most common technique to reverse a string in python. As python string support negative indexing, which help the developer to select the string characters from the end.

    Example:

    #reverse a string using string slicing
    string = "Hello World! Welcome to TechGeekBuzz"
    reverse = string[ : : -1]
    print(reverse)

    Output

    zzuBkeeGhceT ot emocleW !dlroW olleH 

    2. Reverse a String using the While loop

    Using a while loop we can traverse from the last index value of the string to the 0 th index value. With each iteration we save the value in a temporary string and when the iteration end we would print that string.

    Example

    string = "Hello World! Welcome to TechGeekBuzz"
    reverse =""
    
    last_index_value = len(string)-1
    
    while last_index_value>=0:
        reverse += string[last_index_value]
        last_index_value -= 1
        print(reverse)

    Output

    zzuBkeeGhceT ot emocleW !dlroW olleH

    < Note >: Similarly, we can use a for loop to reverse a string

    Example

    string = "Hello World! Welcome to TechGeekBuzz"
    reverse =""
    
    for i in range(len(string)-1, -1, -1):
        reverse += string[i]
        print(reverse)

    Output

    zzuBkeeGhceT ot emocleW !dlroW olleH 

    3. Python string method join() and reversed() function

    • join(): join() is the python string method which accepts an iterable object, join all its elements and return a string.
    • reverse() is a python inbuilt function which accepts an iterable object(string, list, tuple, etc) and returns an iterator object in reverse order.

    Example

    string = "Hello World! Welcome to TechGeekBuzz"
    reverse ="".join(reversed(string))
    print(reverse)

    Output

    zzuBkeeGhceT ot emocleW !dlroW olleH

    4.  Reverse a Python list use join() and list() functions

    • list() is a python inbuilt type conversion function which can convert an iterable object to a list.
    • join(): join() is the python string method which accepts an iterable object, join all its elements and return a string.

    Example

    string = "Hello World! Welcome to TechGeekBuzz"
    reverse ="".join(list(string)
    print(reverse)

    Output

    zzuBkeeGhceT ot emocleW !dlroW olleH 

    5. Reverse a Python list using Recursion

    In recursion, a function keeps calling itself until a base condition gets satisfied.

    Example

    def reverse_str(s):
        if len(s) == 0:
            return s
        else:
            return reverse_str(s[1:]) + s[0]
    
    string = "Hello World! Welcome to TechGeekBuzz"
    reverse =reverse_str(string)
    print(reverse)

    Output

    zzuBkeeGhceT ot emocleW !dlroW olleH

    Conclusion

    An algorithm would be considered the best if it gives the result in the least time or it has the best time complexity. All the techniques we have mentioned above among those the slicing technique gives result in the least time. slicing < list() and join < reversed() and join() < for loop < while loop < recursion.

    People are also reading:

    Leave a Comment on this Post

    0 Comments