4 ways to reverse a string in python

4 ways to reverse a string in python:

In this post, we will learn how to reverse a string in python in different ways. For example, if the string is apple, once reversed, it will be elppa. Our program will take one string as input from the user and print the reversed string.

Python string doesn’t provide any inbuilt method to reverse it. So, we need to write our own method to do that. Let’s take a look at the methods one by one:

Method 1: Using a for or while loop:

This is the easiest way to solve this is by using a loop. We can iterate through the characters of the string one by one and keep it adding to the start of a final result string. We will initialize this as an empty string. Once the loop will end, the final string will hold the reversed string.

Let’s take a look at the program:

def reverse_str(str):
    rev_str = ''
    for c in str:
        rev_str = c + rev_str
    return rev_str


given_str = input('Enter a string : ')
print('Reversed string is : {}'.format(reverse_str(given_str)))

Here,

  • reverse_str is the method that takes one string and returns the reversed string.
  • It iterates through the characters of the provided string one by one and builds the reversed string rev_str. rev_str is initialized as an empty string. Each character is added to the start of rev_str. It creates one new string and we are assigning that new string to rev_str.
  • Finally, after the for loop ends, it returns rev_str.

If you run this program, it will print outputs as like below:

Enter a string : apple
Reversed string is : elppa

Method 2: Recursive approach:

We can also solve this recursively. A recursive method calls itself again and again until a condition is meet. Below program uses recursive approach to reverse a string:

def reverse_str(str):
    if len(str) == 0:
        return str
    else:
        return reverse_str(str[1:]) + str[0]

given_str = input('Enter a string : ')
print('Reversed string is : {}'.format(reverse_str(given_str)))

Here,

  • reverse_str is used to reverse a string recursively.
  • It calls itself by slicing the string from character 1 to end and adds the character 0 to end. This method will stop only if the length of string is 0. So, it will finally create one string with all the characters added in reversed order.

It will print similar output as the above program.

Method 3: By using reversed() method:

reversed method is used to get a reversed iterator for the characters of a string. We can use join to join these characters to create a reversed string.

Below program shows how it works:

def reverse_str(str):
    return ''.join(reversed(str))

given_str = input('Enter a string : ')
print('Reversed string is : {}'.format(reverse_str(given_str)))

The join method joins the characters with the empty string to create the reversed string.

Method 4: By using slicing:

We can use string slicing to reverse a string.

def reverse_str(str):
    return str[::-1]

given_str = input('Enter a string : ')
print('Reversed string is : {}'.format(reverse_str(given_str)))

For string slicing, we can pass start, stop and step values. If we don’t provide any value to start, stop and give -1 to step, it start from end of the string and stop at the start in reverse order. So, it basically creates a reversed string.

If you run this program, it will print the same output.

You might also like: