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.
How to reverse a string in Python?
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 –
1. Reverse a string with slicing
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.
2. Reverse a string using reversed()
and join()
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 –
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
Tutorials on python strings:
- Python – Trim Whitespace Characters from Strings
- Python – Reverse a String with Examples
- Python – Remove Character From String
- Python – Frequency of each word in String
- Python – Convert List to a String
- Python String Count – With Examples
- New String Functions in Python 3.9
- Python String Find – With Examples
- Python String Startswith – With Examples
- Python String Endswith – With Examples
- Python String Split – With Examples
- Python String Join – With Examples
- Python String Concatenation – With Examples
- Python String Replace – With Examples
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python String Strip – With Examples