Python Programming (138 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (21 Blogs)
  • Mastering Python (81 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)

How To Reverse A String In Python?

Last updated on Mar 10,2023 3.8K Views

31 / 62 Blog from Python Fundamentals

Python programming language is abundant in optimal solutions for problems that take a lot of effort and complex code in other programming languages. One of the reason python has picked up a lot of popularity in the last decade was because of the readability and easy syntax that it comes with. One such concept is reversing a string in python. Data Science with Python has a lot of solutions for this particular problem. In this blog, we will discuss various ways to reverse a string in python. The following topics are discussed in this article:

What Is A String?

A string is an immutable data type in python which cannot be changed once we declare it in a program. We use single or double quotes to declare a string in python. Following is an example to show how you can declare a string in python.

name = "edureka"
course = 'python'
Output: edureka

Indexing In Strings

To access the value in a string, we can use indexes. Indexes are locations for specific characters in a string. For example, if we have a string “edureka”, the index at the character ‘e’ will be 0 and at the end of the string the index will be 6.

indexing - how to reverse a string in python- edureka

name = "edureka'

Output: e

How To Reverse A String In Python?

  • Using Recursion
def rev(x):
      str = ""
      for i in s:
          str = i + str
      return str

s = "edureka"
Output: akerude
def rev(s):
     if len(s) == 0:
         return s
         return rev(s[1:]) + s[0]

s = "edureka"
Output: akerude
  • Extended Slice Syntax
name = "edureka"
Output: akerude
def rev(s):
     s = "".join(reversed(s))
     return s

str = "edureka"
Output: akerude

In the above examples, we have reversed the string using different approaches. Python programming language works have a lot of applications in machine learning, artificial intelligence, data science, etc. With optimal functions and concepts, it becomes easier to work with python with efficient results. The increasing demand has catered to a lot of job opportunities for software professionals, which makes it extremely important to learn python. To master all the fundamental concepts enroll in edureka’s python certification program and kick-start your learning.

Have any questions? Mention them in the comments section, we will get back to you as soon as possible.

Upcoming Batches For Python Programming Certification Course
Course NameDate
Python Programming Certification Course

Class Starts on 23rd December,2023

23rd December

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 20th January,2024

20th January

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Reverse A String In Python?