Python String rfind ()

Metóda rfind () vráti najvyšší index podreťazca (ak sa nájde). Ak sa nenájde, vráti -1.

Syntax rfind()je:

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

rfind () parametre

rfind() metóda má maximálne tri parametre:

  • sub - Je to podreťazec, ktorý sa má vyhľadať v reťazci str.
  • začiatok a koniec (voliteľné) - podreťazec sa vyhľadáva v rámcistr(start:end)

Návratová hodnota z rfind ()

rfind() metóda vráti celočíselnú hodnotu.

  • Ak vo vnútri reťazca existuje podreťazec, vráti najvyšší index, kde sa podreťazec nachádza.
  • Ak podreťazec vo vnútri reťazca neexistuje, vráti hodnotu -1.
Návratová hodnota z rfind ()

Príklad 1: rfind () S argumentom No start and end

 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")

Výkon

 Podreťazec „let it“: 22 Podreťazec „small“: -1 Obsahuje podreťazec „be“

Príklad 2: rfind () S argumentmi start a end

 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))

Výkon

 -1 25 -1 18

Zaujímavé články...