HomeLinuxPython Discover Final Incidence in String

Python Discover Final Incidence in String


Discovering the final incidence of a substring in a Python string is a vital job when working with textual content knowledge. The time period “substring” refers to sequences of characters inside a selected string. For instance, within the string “Linux trace”, the sequence of characters “trace” corresponds to a substring. This Python tutorial exhibits you learn how to get the index of the final incidence of a personality in a Python string using completely different strategies.

Tips on how to Get/Discover the Final Incidence in String in Python?

There are lots of methods to find out the ultimate/final incidence of a string character in Python. There are a number of frequent strategies, together with:

Technique 1: Making use of the “rfind()” Technique to Discover Final Incidence in String

The “rfind()” is a built-in technique that’s particularly designed to find the final incidence of a substring in a string.

Syntax

string.rfind(substring, begin=0, finish=None)

 

Within the above syntax:

  • The “substring” parameter specifies the character or substring of which you need to find the final incidence.
  • The “begin” parameter specifies the index from which to start looking.
  • The “finish” parameter specifies the index at which you need to cease/finish looking. The search will go on till it reaches the string finish except you outline the “finish” parameter.

If the desired character or substring seems within the string, the “rfind()” technique offers the index of its final incidence. In any other case, it offers “-1”.

Instance

The next code will discover the final incidence of the character:

string_value = “Welcome to Python Information”
print(string_value.rfind(‘e’))

 

Within the above code, the “string.rfind()” technique accepts the actual character as an argument and finds its final incidence within the string.

Output

The final incidence of the actual character within the outlined string has been displayed on this snippet.

Technique 2: Making use of the “rindex()” Technique to Discover Final Incidence in String

The “rindex()” is one other built-in technique that may be utilized to find out the final/closing incidence of a personality/substring in a given string.

Syntax

string.rindex(worth, begin, finish)

 

Within the above syntax, “worth” is the substring to be searched, and “begin” and “finish” are optionally available parameters that specify the vary of the string to be searched.

Word: This technique will elevate an exception if the substring/ isn’t exists/discovered.

Instance

This code finds the final incidence of the character:

string_value = “Welcome to Python Information”
print(string_value.rindex(‘e’))

 

The above code applies the “rindex()” technique that takes the character as a parameter and retrieves the index of the final incidence within the string.

Output

The above snippet shows the final incidence of the focused character.

Technique 3: Making use of the “str.rpartition()” Technique to Discover Final Incidence in String

The “str.rpartition()” technique will also be utilized to find the final incidence of a personality/substring in a string.

Syntax

string.rpartition(substring)

 

Within the above syntax, “substring” specifies the character or substring that must be evaluated for the final incidence.

Return Worth

The “str.rpartition()” technique returns a tuple of three components:

  • The primary component is the half/piece of the string earlier than the ultimate/final incidence of the enter character.
  • The second component is the final incidence of the enter substring/character.
  • The third component is the string half after the final/closing incidence of the outlined character.

Instance

Right here is the code that finds the final incidence of the character:

string_value = “Welcome to Python Information”
print(string_value.rpartition(‘e’))

 

Within the above code, the “rpartition()” technique takes the said character as an argument and returns the tuple containing three components: a part of the string earlier than the incidence, the final incidence of the desired character, and the worth after the final incidence.

Output

All of the related values are returned appropriately.

Technique 4: Making use of the “for” Loop to Discover/Get the Final String Incidence

The “for” loop will also be utilized to get the ultimate/final incidence of a personality/substring in an enter string.

Instance

To search out the final occasion of the character, use the given code:

string_value = “Welcome to Python Information”
for i in vary(len(string_value)1, –1, –1):
    if string_value[i] == ‘e’:
        print(i)
        break

 

Within the above code traces:

  • The string is outlined and assigned to the variable named “string_value”.
  • The “for” loop iterates over the indices of the string worth in reverse order utilizing the “vary()” and “len()” capabilities.
  • The “if” assertion is employed to examine/confirm if the character on the present index is “e” and if the situation is true, the index is exhibited to the console.

Output

Conclusion

To search out the final incidence within the string, varied strategies such because the “rfind()” technique, “rindex()” technique, “str.rpartition()” technique, or “for” loop are utilized in Python. This weblog demonstrated the approaches to discovering the ultimate/final incidence in a string.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments