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

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (66 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How To Reverse A String In Python?

Last updated on Nov 27,2019 484 Views
30 / 60 Blog from Python Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

Comments
0 Comments

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.