Metóda find () vráti index prvého výskytu podreťazca (ak sa nájde). Ak sa nenájde, vráti -1.
Syntax find()
metódy je:
str.find (sub (, start (, end)))
Parametre pre metódu find ()
find()
Metóda trvá maximálne troch parametrov:
- sub - Je to podreťazec, ktorý sa má vyhľadať v reťazci str.
- začiatok a koniec (voliteľné) - Rozsah,
str(start:end)
v ktorom sa prehľadáva podreťazec.
Návratová hodnota z metódy find ()
find()
Metóda vráti celé číslo:
- Ak podreťazec existuje vo vnútri reťazca, vráti index prvého výskytu podreťazca.
- Ak podreťazec vo vnútri reťazca neexistuje, vráti hodnotu -1.
Fungovanie metódy find ()

Príklad 1: find () S argumentom No start and end
quote = 'Let it be, let it be, let it be' # first occurance of 'let it'(case sensitive) result = quote.find('let it') print("Substring 'let it':", result) # find returns -1 if substring not found result = quote.find('small') print("Substring 'small ':", result) # How to use find() if (quote.find('be,') != -1): print("Contains substring 'be,'") else: print("Doesn't contain substring")
Výkon
Podreťazec „let it“: 11 Podreťazec „small“: -1 Obsahuje podreťazec „be“
Príklad 2: find () S argumentmi start a end
quote = 'Do small things with great love' # Substring is searched in 'hings with great love' print(quote.find('small things', 10)) # Substring is searched in ' small things with great love' print(quote.find('small things', 2)) # Substring is searched in 'hings with great lov' print(quote.find('o small ', 10, -1)) # Substring is searched in 'll things with' print(quote.find('things ', 6, 20))
Výkon
-1 3 -1 9