Python | First character occurrence from rear String
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.
Python3
test_str = "Geeksforgeeks"
print ( "The original string is : " + str (test_str))
res = test_str.rfind( 'e' )
print ( "The index of last element occurrence: " + str (res))
|
Output :
The original string is : Geeksforgeeks
The index of last element occurrence: 10
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1).
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.
Python3
test_str = "Geeksforgeeks"
print ( "The original string is : " + str (test_str))
test_str = list (test_str)
res = len (test_str) - 1 - test_str[:: - 1 ].index( 'e' )
print ( "The index of last element occurrence: " + str (res))
|
Output :
The original string is : Geeksforgeeks
The index of last element occurrence: 10
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using max() method
Python3
test_str = "Geeksforgeeks"
print ( "The original string is : " + str (test_str))
x = []
for i in range ( 0 , len (test_str)):
if (test_str[i] = = "e" ):
x.append(i)
res = max (x)
print ( "The index of last element occurrence: " + str (res))
|
Output
The original string is : Geeksforgeeks
The index of last element occurrence: 10
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(m), where m is the number of occurrences of the character “e” in the input string.
Method #4: Using index() method
Python3
test_str = "Geeksforgeeks"
reverse_str = test_str[:: - 1 ]
print ( "The original string is : " + str (test_str))
res = reverse_str.index( 'e' )
res = len (test_str) - res - 1
print ( "The index of last element occurrence: " + str (res))
|
Output
The original string is : Geeksforgeeks
The index of last element occurrence: 10
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Python code to demonstrate First character occurrence from rear String using Regex
Python3
import re
test_str = "Geeksforgeeks"
print ( "The original string is : " + str (test_str))
match = re.search(r 'e(?!.*e)' , test_str)
res = match.start()
print ( "The index of last element occurrence: " + str (res))
|
Output
The original string is : Geeksforgeeks
The index of last element occurrence: 10
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using a loop to iterate through the string from the end
Start iterating through the string from the end using a loop. Check each character and if we find the first occurrence of the character we’re looking for, store the index and break out of the loop.
Python3
test_str = "Geeksforgeeks"
print ( "The original string is : " + str (test_str))
for i in range ( len (test_str) - 1 , - 1 , - 1 ):
if test_str[i] = = 'e' :
res = i
break
print ( "The index of last element occurrence: " + str (res))
|
Output
The original string is : Geeksforgeeks
The index of last element occurrence: 10
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(1).
Last Updated :
15 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...