In this tutorial, we will look at how to get the last index of a character in a Python string with the help of some examples.
A character may appear multiple times in a string. For example, in the string “banana”, the character “a” occurs three times – at indexes 1, 3, and 5 respectively. We are looking for a way to get its last index. For example, 5 in this case.
Note that, characters in a string are indexed starting from 0 in Python.
How to get the last index of a character in a string?
There are built-in string functions in Python like find()
and index()
to get the index of the first occurrence of a character but there isn’t one for getting the index of its last occurrence.
To get the last index of a character in a string in Python, subtract its first index in the reversed string and one from the length of the string.
Let’s look at a couple of ways to perform the above operation with the help of some examples.
Using string slice with a -1 step size
To get the last index of a character in the string –
- Reverse the string using the slice operation with a -1 step size. For example, for string
s
,s[::-1]
will give the reversed string. - Find the first index of the character in the reversed string using the string
index()
function. - Subtract the above index and 1 from the length of the string to get the last index of the character in the original string.
# create a string s = "banana" # reverse the string rev_s = s[::-1] # get last index of the req. character print(len(s) - rev_s.index("a") - 1)
Output:
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.
5
You can see that we get the last index of “a” in “banana” as 5.
Using the reversed()
function
To get the last index of a character in the string –
- Use the Python built-in
reversed()
function to get the list of characters in the string in the reveresd order and then using the stirngjoin()
function to join them together in a string. This will give you the reversed string. - Find the first index of the character in the reversed string using the string
index()
function. - Subtract the above index and 1 from the length of the string to get the last index of the character in the original string.
# create a string s = "banana" # reverse the string rev_s = "".join(reversed(s)) # get last index of the req. character print(len(s) - rev_s.index("a") - 1)
Output:
5
We get the same result as above.
For more on the reversed() function, refer to its documentation.
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.