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

Data Science

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

How To Reverse A String In Python?

Last updated on Nov 27,2024 4.2K 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'
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 Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 28th December,2024

28th December

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

Class Starts on 25th January,2025

25th January

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