Reverse A String In Python

In this tutorial, I will teach you how to reverse a string in python. Moreover, I will discuss 5 different methods to solve the problem. So, let’s get started.

1. Reverse A String In Python using slicing

Here we are going to use the slicing feature of python to reverse a string.

def reverse(string):
    return string[::-1]

string = input("Enter String To Reverse : ")
r = reverse(string)
print("Reverse : ",r)

Explanation :

String [::-1] follows the syntax string[start:stop:step] .In this case, the value of start and stop is none which means it will take the whole string from start to end.but the value of step is -1 which implies it will start from the end, therefore, reversing the string.

Save and run the above code you should see the following output.

Output :

reverse a string in python code example output

2. Reverse A String In Python using for loop

Now let’s use the for loop to reverse the string.

def reverse(string):
    rev = ''
    for i in string:
        rev = i + rev
    return rev

string = input("Enter String To Reverse : ")
r = reverse(string)
print("Reverse : ",r)

Explanation :

Inside the function reverse. first, we create the variable of name rev and set it to an empty string then we loop through the input string, and in each iteration, we add the character to the end of the variable rev, therefore, reversing the string.

3. Reverse A String In Python using while loop

def reverse(string):
    rev = ''
    index = len(string) - 1
    while index >= 0:
        rev += string[index]
        index = index - 1
    return rev

string = input("Enter String To Reverse : ")
r = reverse(string)
print("Reverse : ",r)

Explanation :

  • Inside the function, first, we created a variable of name rev and set it to an empty string then we created the second variable of the name index and set it to one less than the length of the input string.
  • Then we define the while loop with the condition that the code inside the loop will run until the value of the variable index is greater than equal to one.
  • Inside the body of the while loop, first, we update the variable rev by adding the character at string[index] then we decrease the value of the variable index by 1.
  • Then we return the reversed string to the caller of the function.

4. Reverse a string using the list

def reverse(string):
    lis = list(string)
    lis.reverse()
    return ''.join(lis)

string = input("Enter String To Reverse : ")

r = reverse(string)
print("Reverse : ",r)

Explanation :

First, we convert the string to a list. then we reverse the list using the reverse() method. in the end, we return and convert the reversed list to the string using the join method.

5. Reverse a string using recursion

def reverse(s):
    total = len(s)

    if total  == 0:
        return ''

    last_char = s[total-1]
    last_skipped = s[:total-1]
    return last_char + reverse(last_skipped)

Explanation :

  • Inside the recursive function, first, we defined the variable of name total and set it equal to the length of the input string.
  • Comes the base condition where we check if the value of total is 0 then we return an empty string.
  • we create a variable last_char and set it to the last character of the input string.
  • then we grab the whole string skipping its last character and set it to variable last_skipped.
  • Finally, we return the last character plus call the reverse function recursively to the sliced string.

That’s the wrap for this tutorial.

Also read about how to find prime number in python.

Leave a Reply