Reverse a python string

Python – Reverse a String with Examples

Strings in Python are ordered sequences of characters. For example, “carpet”. Now, if you want to reverse this string, how would you do it? In this tutorial, we will look at how to reverse a string in python with the help of some examples.

There is no built-in string function to reverse a string in python. You can, however, do it using a number of different methods.

For example, you can use the slicing operator with a -1 step size, or you can use reversed() and string join() functions in combination to reverse a string. Let’s look at these methods with the help of examples –

Slicing is generally used in python to get a sub-section of an ordered collection, for example, string, list, etc. We provide the start and the end index and get a sliced output. For example, getting only the first three characters of a string with [:3].

You can also use slicing to reverse a string. We do so by passing a step size of negative one to the slicing operation. For example, let’s reverse the string “carpet” using this method –

# create a string
s = "carpet"
# reverse the string 
print(s[::-1])

Output:

teprac

We see that the resulting string is reverse of the original. Note that the syntax for slicing a string is s[start:stop:step], generally we are only concerned with the start and stop indexes when slicing a string. But, in this particular use-case, if you slice for the entire string with a -1 step size, you get the original string reversed.

You can also reverse a string in python by first converting it to an iterator of characters in the reverse order using the in-built reversed() function and then concatenate the characters back together using the string join() function. For example, let’s reverse the string “carpet” using this method –

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# create a string
s = "carpet"
# iterator of characters in reverse order
rev_s = reversed(s)
# concatenate the characters back
print("".join(rev_s))

Output:

teprac

You can see that the original string has been reversed from “carpet” to “teprac”.

For more on the reversed() function in python, refer to its documentation.

The above-mentioned methods are surely not an exhaustive list. You can be creative and come with with a number of different ways to reverse a string python.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top