Skip to content
Home / Fundamentals

How to Reverse a String in Python

A string is a sequence of characters, and reversing a string means to rearrange the characters in the opposite order. In Python, there are multiple ways to reverse a string, depending on your requirements and the constraints of your specific use case.

Method 1: Using Slicing

One of the simplest ways to reverse a string in Python is by using slicing. Slicing is a feature in Python that allows you to extract a portion of a sequence (such as a string, list, or tuple) by specifying a start and end index.

To reverse a string using slicing, you can specify a range that starts at the end of the string and ends at the beginning, and then step through the range with a negative step size of -1. For example:

def reverse_string_slicing(s):
    return s[::-1]

# Test the function
print(reverse_string_slicing("hello"))  # Output: "olleh"
print(reverse_string_slicing("Python"))  # Output: "nohtyP"

Method 2: Using a For Loop

Another way to reverse a string in Python is by using a for loop. To do this, you can create an empty string called reversed_string, and then iterate over the original string in reverse order, adding each character to the reversed_string variable.

Here is an example of how to reverse a string using a for loop:

def reverse_string_for_loop(s):
    reversed_string = ""
    for char in s[::-1]:
        reversed_string += char
    return reversed_string

# Test the function
print(reverse_string_for_loop("hello"))  # Output: "olleh"
print(reverse_string_for_loop("Python"))  # Output: "nohtyP"

Method 3: Using Recursion

Another way to reverse a string in Python is by using recursion, which is a technique for solving a problem by breaking it down into smaller and smaller subproblems until you reach a base case.

To reverse a string using recursion, you can define a function that takes the string as an argument and returns the reversed string. If the string is empty, you can return an empty string. Otherwise, you can return the last character of the string concatenated with a recursive call to the function with the string minus the last character as an argument.

Here is an example of how to reverse a string using recursion:

def reverse_string_recursion(s):
    if s == "":
        return s
    else:
        return reverse_string_recursion(s[1:]) + s[0]

# Test the function
print(reverse_string_recursion("hello"))  # Output: "olleh"
print(reverse_string_recursion("Python"))  # Output: "nohtyP"

Method 4: Using the Built-in reversed() Function (cont.)

To reverse a string using the reversed() function, you can pass the string as an argument to the function and then use the join() method to join the reversed characters together into a new string. For example:

def reverse_string_reversed(s):
    return "".join(reversed(s))

# Test the function
print(reverse_string_reversed("hello"))  # Output: "olleh"
print(reverse_string_reversed("Python"))  # Output: "nohtyP"

Method 5: Using the join() and reversed() Functions

You can also reverse a string in Python by using the join() and reversed() functions together. To do this, you can first use the reversed() function to reverse the characters of the string, and then use the join() function to join the reversed characters together into a new string.

Here is an example of how to reverse a string using the join() and reversed() functions:

def reverse_string_join_reversed(s):
    return "".join(reversed(s))

# Test the function
print(reverse_string_join_reversed("hello"))  # Output: "olleh"
print(reverse_string_join_reversed("Python"))  # Output: "nohtyP"