last index of character "a" in string "banana"

Python – Get Last Index of Character in String

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.

last index of character "a" in string "banana"

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.

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.

To get the last index of a character in the string –

  1. Reverse the string using the slice operation with a -1 step size. For example, for string s, s[::-1] will give the reversed string.
  2. Find the first index of the character in the reversed string using the string index() function.
  3. 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:

📚 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.

5

You can see that we get the last index of “a” in “banana” as 5.

To get the last index of a character in the string –

  1. Use the Python built-in reversed() function to get the list of characters in the string in the reveresd order and then using the stirng join() function to join them together in a string. This will give you the reversed string.
  2. Find the first index of the character in the reversed string using the string index() function.
  3. 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 –

  1. Python – Reverse a String with Examples
  2. Python – Remove the Last Word From String


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