Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Methods of Reversing a String in Python
2.1.
1. Reverse a string in Python using a loop
2.2.
Python
2.3.
2. Reverse a string in Python using recursion
2.4.
Python
2.5.
3. Reverse a string in Python using Stack
2.6.
Python
2.7.
4. Reverse a string using an extended slice
2.8.
Python
2.9.
5. Reverse a string using reversed() method
2.10.
Python
2.11.
6. Reverse a string in Python using list comprehension Method()
2.12.
Python
2.13.
7. Reverse a string using the function call
2.14.
Python
3.
Frequently Asked Questions
3.1.
How can you reverse a string using Python? 
3.2.
How does reverse () work Python?
3.3.
How do you reverse a string in a list Python?
4.
Conclusion
Last Updated: Jun 18, 2024
Easy

How to Reverse a String in Python (7 Different Ways)

Author Anant Dhakad
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Python comes with tons of libraries and inbuilt functions. There are libraries for data science, machine learning, maths etc. Often these inbuilt function comes in handy when doing a certain task in Python. Take the case of string reversal in Python. There are different ways to reverse a string in Python

how to reverse a string in python

In this blog, we will learn How to Reverse a String in Python.

You can also read about the Multilevel Inheritance in Python, Swapcase in Python

Methods of Reversing a String in Python

1. Reverse a string in Python using a loop

In this method, the string is revered using Python ‘for loop’. We iterate over all the characters in the strings and concatenate them in reverse order.

Program
 

  • Python

Python

# Method 1

""" Using loop """

def reverse(str):
   rev_str = ""
   for c in str:
       rev_str = c + rev_str
   return rev_str

s = "Coding Ninjas"
print("Original String is: ", s)
print("[Using loop]Reversed String is: ", reverse(s))


Output

Original String is:  Coding Ninjas
[Using loop]Reversed String is:  sajniN gnidoC


2. Reverse a string in Python using recursion

In this method, the string is reversed using recursion. The base case is when only a single character is left in the string. 

Program

  • Python

Python

# Method 2

""" Using recursion """

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

s = "Coding Ninjas"
print("Original String is: ", s)
print("[Using recursion]Reversed String is: ", reverse(s))


Output

Original String is:  Coding Ninjas
[Using recursion]Reversed String is:  sajniN gnidoC


3. Reverse a string in Python using Stack

In this method, the string is reversed using “stack.” We will take an empty list as a stack and push all the characters in it. The elements from the reversed order will be at the top of the stack. We will add the elements from the top to our empty string and pop the characters from the stack until it becomes empty.

Program

  • Python

Python

# Method 3

"""""using stack"""""

mystring = "Python Compiler"
print("original string is : ", mystring)

# take an empty string
reversed_string = "";

# push all the characters in stack
stack = []
for element in mystring:
  stack.append(element)

# access the characters one by one from the top of stack
while(len(stack) > 0):
  current_character = stack.pop()
  reversed_string += current_character

print("reversed string is : ", reversed_string)


Output

original string is :  Python Compiler
reversed string is :  relipmoC nohtyP


4. Reverse a string using an extended slice

In this method, the string is reversed using the Python slice operator. 

Program

  • Python

Python

# Method 4

""" Using extended slice operator """

s = "Coding Ninjas"
print("Original String is: ", s)
print("[Using extended slice operator]Reversed String is: ", s[::-1])


Output

Original String is:  Coding Ninjas
[Using extended slice operator]Reversed String is:  sajniN gnidoC


5. Reverse a string using reversed() method

Here we are using the inbuilt reversed() function. It will return a list of characters in reversed order. We will join these characters into a resultant string.

Program

  • Python

Python

# Method 5

"""""using reversed method"""""

# original string
mystring = "Resources"
print("original string is : ", mystring)

# create an empty string
reversed_string = ""
reversed_characters = reversed(mystring);
reversed_string = "".join(reversed_characters);

print("reversed string is : ", reversed_string)


Output

original string is :  Resources
reversed string is :  secruoseR


6. Reverse a string in Python using list comprehension Method()

In this method, the string is reversed using a list comprehension method. We will create a new list by iterating over the string in reverse order. At last we will join all the elements of a list into a single string.

Program

  • Python

Python

# Method 6

"""""using list comprehension"""""

mstring = "contests and Events"
print("original string is : ", mstring)

# take an empty string
reversed_string = "";

# take list containing all the characters of our string in reverse order
mylist = [mstring[i] for i in range(len(mstring) - 1, 0, -1)]
reversed_string = "".join(mylist)

print("reversed string is : ", reversed_string)


Output

original string is :  contests and Events
reversed string is :  stnevE dna stsetnoc


7. Reverse a string using the function call

In this method, we will reverse a string using the function call. The function will take the original string as a parameter and return the reversed string. Here, we’ve used slicing technique to reverse a string inside a function.

Program

  • Python

Python

# Method 7

"""""using a function call"""""

# function to reverse a string
def Reverse(s):
  return s[::-1];

mstring = "Online Compiler"
print("original string is : ", mstring)

# call Reverse function
reversed_string = Reverse(mstring);

print("reversed string is : ", reversed_string)


Output

original string is :  Online Compiler
reversed string is :  relipmoC enilnO


You can practice by yourself with the help of online python compiler.

You can also check out Reverse A String In C

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

How can you reverse a string using Python? 

You can reverse a string in Python using slicing: reversed_string = original_string[::-1].

How does reverse () work Python?

reverse() is a method for lists in Python, reversing the order of elements in place.

How do you reverse a string in a list Python?

Use reverse() method for lists: my_list.reverse() reverses the order of elements within the list.

Conclusion



In this article, we learnt How to Reverse a String in Python

Check out this article - String slicing in Python

Recommended Readings:

Recommended problems -

 

We hope that this blog has helped you enhance your knowledge regarding reversing strings in python and if you would like to learn more, check out our articles on the Code360. Also, do check out our course on C++.

Previous article
String Reverse in Python
Next article
F-Strings in Python
Live masterclass