There are many ways to find out the first index of element in String as python in its language provides index() function that returns the index of first occurrence of element in String. But if one desires to get the last occurrence of element in string, usually a longer method has to be applied. Lets discuss certain shorthands to achieve this particular task.
Method #1 : Using rfind()
This is usually the hack that we can employ to achieve this task. Employing string function rfind() to get the first element from right i.e last index of element in String.
# Python 3 code to demonstrate # First character occurrence from rear String # using rfind() # initializing string test_str = "Geeksforgeeks" # printing original string print ("The original string is : " + str(test_str)) # using rfind() # to get last element occurrence res = test_str.rfind('e') # printing result print ("The index of last element occurrence: " + str(res)) |
The original string is : Geeksforgeeks The index of last element occurrence: 10
Method #2 : Using List Slice + index() + list()
One can convert the string to list using list() and then using list slicing we reverse the list and use the conventional index method to get the index of first occurrence of element. Due to reversed list, the last occurrence is returned rather than the first index of list.
# Python 3 code to demonstrate # First character occurrence from rear String # using List Slice + index() + list() # initializing string test_str = "Geeksforgeeks" # printing original string print ("The original string is : " + str(test_str)) # using List Slice + index() + list() # First character occurrence from rear String test_str = list(test_str) res = len(test_str) - 1 - test_str[::-1].index('e') # printing result print ("The index of last element occurrence: " + str(res)) |
The original string is : Geeksforgeeks The index of last element occurrence: 10
Recommended Posts:
- Python - Rear character String categorization
- Python | Rear stray character String split
- Python | Strings with similar front and rear character
- Python - Sort by Rear Character in Strings List
- Python | Replace rear word in String
- Python | Kth Non-None String from Rear
- Python - Remove Rear K characters from String List
- Python - Strip front and rear Punctuations from given String
- Generate two output strings depending upon occurrence of character in input string in Python
- Python program to find occurrence to each character in given string
- Python | Split string on Kth Occurrence of Character
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Python - Extract String after Nth occurrence of K character
- Python | Front and rear range deletion in a list
- Python | Append at front and remove from rear
- Access front and rear element of Python tuple
- Python - Remove rear element from list
- Python - Rear element extraction from list of tuples records
- Python - Test rear digit match in all list elements
- Python | Alternate front - rear Sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

