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

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (26 Blogs)
  • Mastering Python (92 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

How To Reverse A String In Python?

Last updated on Nov 27,2019 3.3K 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. 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'
print(name)
print(course)
Output: edureka
        python

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'
print(name[4])

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"
print(rev(s))
Output: akerude
def rev(s):
     if len(s) == 0:
         return s
     else:
         return rev(s[1:]) + s[0]

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

str = "edureka"
print(rev(str))
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 Certification Training Course
Course NameDate
Python Certification Training Course

Class Starts on 28th January,2023

28th January

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

Class Starts on 25th February,2023

25th February

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

Class Starts on 25th March,2023

25th March

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
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?

edureka.co