Hey! If you love Python and building Python apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

How to Reverse a String in Python

Learn how to reverse a string in Python using various methods, including slicing, recursion, and built-in functions. …


Updated May 3, 2023

Learn how to reverse a string in Python using various methods, including slicing, recursion, and built-in functions.

Definition of the Concept

In programming, reversing a string means swapping the order of its characters. For example, if you have the string “hello”, reversing it would result in “olleh”. This concept is useful in many scenarios, such as:

  • Reading input from users and processing it in reverse order
  • Generating passwords or encryption keys
  • Creating reversed copies of text for debugging purposes

Step-by-Step Explanation: Reversing a String Using Slicing

One way to reverse a string in Python is by using slicing. Here’s the step-by-step process:

1. Split the String into Two Parts

Split the input string into two parts: one from the beginning to the middle, and another from the end to the middle.

def split_string(input_str):
    mid = len(input_str) // 2
    return input_str[:mid], input_str[mid:]

2. Reverse the Two Parts

Reverse each part individually by using slicing again.

def reverse_part(part):
    return part[::-1]

3. Combine the Reversed Parts

Combine the reversed parts to form the final reversed string.

def combine_parts(rev_left, rev_right):
    return rev_left + rev_right

Complete Code: Reversing a String Using Slicing

Here’s the complete code that puts it all together:

def reverse_string_slicing(input_str):
    def split_string(input_str):
        mid = len(input_str) // 2
        return input_str[:mid], input_str[mid:]
    
    def reverse_part(part):
        return part[::-1]
    
    def combine_parts(rev_left, rev_right):
        return rev_left + rev_right
    
    left, right = split_string(input_str)
    reversed_left = reverse_part(left)
    reversed_right = reverse_part(right)
    result = combine_parts(reversed_left, reversed_right)
    return result

input_str = "hello"
print(reverse_string_slicing(input_str))  # Output: olleh

Alternative Method: Using the reversed Function

Python provides a built-in function called reversed that can be used to reverse strings.

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

input_str = "hello"
print(reverse_string_reversed(input_str))  # Output: olleh

Recursive Method

Another way to reverse a string is by using recursion. However, this method can be less efficient than slicing for large strings.

def reverse_string_recursive(input_str):
    if len(input_str) <= 1:
        return input_str
    else:
        return reverse_string_recursive(input_str[1:]) + input_str[0]

input_str = "hello"
print(reverse_string_recursive(input_str))  # Output: olleh

Conclusion

In this article, we explored three different methods for reversing strings in Python. Each method has its own strengths and weaknesses, and the choice of which one to use depends on the specific requirements of your project.

Stay up to date on the latest in Python, AI, and Data Science

Intuit Mailchimp