How to reverse a string in Python [8 different ways]

In this python tutorial, you will learn about the python program to reverse a string.

There are 8 methods to reverse the string in Python, which are shown below.

  • Using the slicing method
  • Using the reversed() function
  • Using For loop
  • Using the Recursion
  • Using join() and list comprehension method
  • Using the reduce() function
  • Using the stack
  • Using the list comprehension
  • Using the while loop

Python program to reverse a string

Let us all 8 methods to reverse a string in Python using examples. And we will start with the first method of using slicing.

Method-1: Python program to reverse a string using the slicing method

This method uses the slicing of Python strings to reverse the string by starting from the last character and returning every character to the first one with a step of -1.

# Function to reverse a string
def reverse_string(string):
    # Returns the reversed string by slicing from the end to the beginning
    return string[::-1]

# Testing the function with the input string "United Kingdom"
print("Reverse of the string is : ", reverse_string("United Kingdom"))

The above code defines a function called reverse_string which takes a string as an argument and returns the reverse of the input string using string slicing. The function is then called and the reverse of the string “United Kingdom” is printed to the console.

Python program to reverse a string
Python reverse string Using the slicing method

Read: How to Check if a String contains a Substring in Python

Method-2: Python program to reverse a string using the reversed() function

The reversed() function returns a reverse iterator, so we can convert it into a string using the join() method. This method is easy to use and more efficient than some other methods.

# Function to reverse a string
def reverse_string(string):
    # Return the reversed string using the reversed() function and join() method
    return "".join(reversed(string))

# Print the reversed string
print("Reverse of the string is : ", reverse_string("USA"))

The above code defines a function reverse_string which takes a string as an argument and returns the reversed version of the string.

The function uses the reversed function and the join method to reverse the string. The code then calls the reverse_string function with the argument “USA” and prints the returned value, which is the reverse of the string “USA”.

Output: Reverse of the string is :  ASU

Read: Python string formatting

Method-3: Python program to reverse a string using For loop

In this method, we use a for loop to iterate over the characters in the string and build a new string by adding each character to the beginning of an empty string.

# Function to reverse a string
def reverse_string(string):
    # Initialize an empty string
    result = ""
    # Loop through each character in the string and add it to the beginning of the result string
    for char in string:
        result = char + result
    # Return the reversed string
    return result

# Call the function with a string "Canada" as an argument
print("Reverse of the string is : ", reverse_string("Canada"))

The above code defines a function reverse_string which takes a string as input and returns the reverse of that string by concatenating each character from the end to the start of the string into a new result string.

  • The function is then called with the string “Canada” and the resulting reverse string is printed.
Outptut: Reverse of the string is :  adanaC

Read: Python compare strings

Method-4: Python program to reverse a string using the Recursion

This method uses recursion to reverse the string. The base case of the recursion is an empty string, and for each recursive call, the first character is added to the end of the reverse of the rest of the string.

# Function to reverse a string using recursion
def reverse_string(string):
    # Base case: If the length of the string is 0, return the string
    if len(string) == 0:
        return string
    else:
        # Recursive step: Return the last character of the string + the reverse of the rest of the string
        return reverse_string(string[1:]) + string[0]

# Testing the reverse_string function with the input "New York"
print("Reverse of the string is : ", reverse_string("New York"))
Output: Reverse of the string is :  kroY weN

Read: Python find number in String

Method-5: Python program to reverse a string using join() and list comprehension method

This method uses the join() method to concatenate the characters in the string in reverse order, generated through a generator expression that iterates over the string in reverse order.

# Function to reverse a string using list comprehension
def reverse_string(string):
    return "".join(string[i] for i in range(len(string)-1, -1, -1))

# Reverse the string "Brazil" and print the result
print("Reverse of the string is : ", reverse_string("Brazil"))

The above code defines a function reverse_string that takes a string as an argument. The function returns a reversed version of the string using the built-in join method.

  • The join method concatenates the elements of the string, which are obtained using the list comprehension that iterates through the string in reverse order (i.e., from the last index to the first index).
Output: Reverse of the string is :  lizarB

Read: Find first number in string in Python

Method-6: Python program to reverse a string using the reduce() function

This method uses the reduce() function from the functools module to apply a binary function to the elements of the string in reverse order, creating a single cumulative value.

# Function to reverse a string using reduce method
from functools import reduce

def reverse_string(string):
    # Using reduce to concatenate the characters in the string in reverse order
    return reduce(lambda x, y: y + x, string)

# Testing the reverse_string function
print("Reverse of the string is : ", reverse_string("Australia"))

The above code is a python script that reverses a string using the reduce() method from the functools library. The script defines a function called reverse_string which takes a string as an argument.

  • The function returns the reversed string by using the reduce() method with a lambda function as an argument.
Output: Reverse of the string is :  ailartsuA

Read: Remove non-ASCII characters Python

Method-7: Python program to reverse a string using the stack

This method uses a stack data structure to reverse the string. The stack is created by pushing each character in the string onto it and then popping the characters off the stack to build the reversed string.

# Function to reverse a string using a stack
def reverse_string(string):
    # Create a stack of characters from the input string
    stack = [char for char in string]
    # Initialize an empty string to store the result
    result = ""
    # Pop characters from the stack and add them to the result until the stack is empty
    while stack:
        result += stack.pop()
    # Return the reversed string
    return result

# Test the function by passing a sample string
print("Reverse of the string is : ", reverse_string("Washington"))

The above code defines a function reverse_string that takes in a string as an argument and returns its reverse.

  • The function uses a stack data structure to store the characters of the string and then pops the elements from the stack to get the reverse string.
  • Finally, the reversed string is returned and printed.
Output: Reverse of the string is :  notgnihsaW

Read: How to trim a string in Python

Method-8: Python program to reverse a string using the while loop

A while loop is used to iterate over the indices of the string in reverse order (from the last character to the first character).

# Function to reverse a string
def reverse_string(string):
    # Initializing an empty string to store the reverse
    result = ""
    # Finding the length of the string
    length = len(string)
    # Iterating through the string in reverse order
    while length > 0:
        # Decreasing the length by 1
        length -= 1
        # Adding the character to the result string
        result += string[length]
    # Returning the reverse string
    return result

# Printing the reverse of the string
print("Reverse of the string is : ", reverse_string("Amazon"))

The above code defines a function reverse_string which takes a string as an argument and returns its reverse by concatenating the characters in reverse order.

  • The reverse string is calculated using a loop that starts from the end of the input string and goes till the start.
  • For each iteration, the character at the current index is added to the result string.
Output: Reverse of the string is :  nozamA

You may also like to read the following Python tutorials.

In this Python tutorial, we have learned about the Python program to reverse a string using the following methods:

  • Using the slicing method
  • Using the reversed() function
  • Using For loop
  • Using the Recursion
  • Using join() and list comprehension method
  • Using the reduce() function
  • Using the stack
  • Using the list comprehension
  • Using the while loop