How to reverse a string in python?


A string in Python is a sequence or a collection of characters enclosed within single or double-quotes. Using print() function you can display a string. In Python, there are various built-in functions to manipulate a string.

There is no function such as reverse() for reversing a string. But there are different ways in which you can reverse a string in Python for example –

  • Using for loop
  • Using while loop
  • With the use of slicing
  • Using reversed() function
  • Using recursion

In this article, we will explain all of them with some examples.

Reverse a string using for loop

We can use a for loop to iterate through each character of a string. And add each of them in reverse order into an empty string. You can see the example which is given below.

# Python program to reverse a string using for loop

def reverseStr(s):
   str1 = ""
   for i in s:
       str1 = i + str1
   return str1

s = "Hello World!"

print (f"The given string is : {s}")

print (f"The reverse of string is : {reverseStr(s)}")

When you execute this program it will produce the given output.

Reverse a string using while loop

Just like a for loop, we can use a while loop to iterate through each character of a string. In each iteration, we will add the character in reverse order in an empty string.

See the given example.

# Python program to reverse a string using while loop

def reverseStr(s):
      str1 = ""
      count = len(s)-1
      while count>=0:
             str1 += s[ count ]
             count = count - 1
      return str1
s = "Hello World!"
print (f"The given string is : {s}")
print (f"The reverse of string is using while loop is : {reverseStr(s)}")

You can see the output of this program in the given image.

Reverse a string using slicing

String slicing is to obtaining a substring from a string that means we can extract a range of characters from a string with string slicing. If you provide -1 as a step argument this will reverse the given string.

Now see the given Python code.

# Python program to reverse a string slicing

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

s = 'Hello World!'
print(f'The given string is: {s}')
print(f'Reverse string using slicing is: {reverseStr(s)}')

When you execute this program it will display the given output.

Reverse a String using reversed() function

Python provides the reversed() function to reverse the string. Let’s understand this with the following example.

#Python program to reverse a string using reversed() function

def reverseStr(str):
    str1 = "".join(reversed(str))
    return str1

s = "Hello World!"

print (f"The original string is : {s}")
print (f"Reverse string using reversed() function is : {reverseStr(s)}")  

On executing this code you will see the given output.

Reverse a string using recursion

Recursion is a process where a function calls itself repeatedly. We will use this to reverse a string in Python.

You can see the example which is given below.

# Python program to reverse a string using recursion

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

s = "Python programming"
print (f"The original string  is :{s} ")
print ("The reversed string using recursion is : {reverseStr(s)}") 

The output of this program is –

Conclusion

Among all of the given methods, the best way to reverse a string in Python is the slicing method because it is faster than all others and also easy to use.

Now if you have a query then write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.