Python String rfind ()

De methode rfind () retourneert de hoogste index van de subtekenreeks (indien gevonden). Indien niet gevonden, retourneert het -1.

De syntaxis van rfind()is:

 str.rfind (sub (, start (, end)))

rfind () Parameters

rfind() methode heeft maximaal drie parameters:

  • sub - Het is de substring die moet worden doorzocht in de str-reeks.
  • begin en einde (optioneel) - substring wordt doorzocht binnenstr(start:end)

Retourwaarde van rfind ()

rfind() methode retourneert een geheel getal.

  • Als een subtekenreeks binnen de tekenreeks bestaat, retourneert deze de hoogste index waarin de subtekenreeks is gevonden.
  • Als substring niet bestaat binnen de string, retourneert deze -1.
Retourwaarde van rfind ()

Voorbeeld 1: rfind () Zonder begin- en eindargument

 quote = 'Let it be, let it be, let it be' result = quote.rfind('let it') print("Substring 'let it':", result) result = quote.rfind('small') print("Substring 'small ':", result) result = quote.rfind('be,') if (result != -1): print("Highest index where 'be,' occurs:", result) else: print("Doesn't contain substring")

Uitvoer

 Substring 'let it': 22 Substring 'small': -1 Bevat substring 'be,'

Voorbeeld 2: rfind () Met begin- en eindargumenten

 quote = 'Do small things with great love' # Substring is searched in 'hings with great love' print(quote.rfind('things', 10)) # Substring is searched in ' small things with great love' print(quote.rfind('t', 2)) # Substring is searched in 'hings with great lov' print(quote.rfind('o small ', 10, -1)) # Substring is searched in 'll things with' print(quote.rfind('th', 6, 20))

Uitvoer

 -1 25-1 18

Interessante artikelen...