Menu Close

Reverse a String in Python ( 5 best ways )

Reverse a string in Python

In this python example tutorial, you will learn how to write a python program to reverse a string. We will see 5 best ways to reverse a string python. Reverse a string in python is a very common question to ask by most of the python programmers as well as the interviewer.

To understand this example you should have basic knowledge of following Python topics.

To understand this example you should have basic knowledge of python programming. If you want to learn basic Python programming then click here.

Python does not provide any built function to reverse a string in python but still
here we will reverse a string in python from different-2 methods.

Python program to reverse a string

Here we will describe 5 different ways to reverse a string in Python.

Reverse a string using slice:

We will write a python program to reverse a string using the slice technique.

mystring = input("Enter your string here:- ")
x = mystring[::-1]
print(x)

Output

Enter your string here:- Programming
gnimmargorP

Reverse a string using slice inside function

In this example we will reverse a string in python using slice method as well as function.

mystring = input("Enter your string here:- ")
def reverse(x):
	result = x[::-1]
	print(result)
	
reverse(mystring)

Output

Enter your string here:- Python
nohtyP

Reverse a String using for loop

In this example we will use for loop to reverse a string in python.

string = input("Enter you string here:- ")
#empty string to store reversed string.
str = ''
#for loop to reverse a string
for i in string:
	str = i + str
print(str)

Output

Enter you string here:- Hello Python!
!nohtyP olleH

Reverse String using recursion

Here we used recursion to write python program to reverse a string.

#Ask string from the user
mystring = input("Enter your string here:- ")

#Function to find reverse a string
def reverse(x):
	if len(x)==0:
		return x
	else:
		return reverse(x[1:]) + x[0]
#print original string	
print("The original string  is : ",end="") 
print(mystring)

#print reversed string
print("The reversed string(using recursion) is : ",end="") 
print(reverse(mystring))

Output

Enter your string here:- My name is Python
The original string  is : My name is Python
The reversed string(using recursion) is : nohtyP si eman yM

Reverse a string using reversed function

#Assign default string to reverse
mystring = "Programming Funda"

#Python function reverse the string
def reverse(x):
	result = reversed(x)
	print(''.join(result))
	
#Call the function
reverse(mystring)

Output will be:- adnuF gnimmargorP

Conclusion

So that in this python example we have seen how to write a python program to reverse a string. We have used the 5 best ways to reverse a string. I hope you don’t have any confusion regarding reverse a string in Python.

If you want to write this type of python program, please keep it continue for further python examples.

Python examples

For more information:- Click here

Previous Article
Next Article

Was this post helpful?

Related Posts

Leave a Reply