Metoda find () vrací index prvního výskytu podřetězce (je-li nalezen). Pokud není nalezen, vrátí hodnotu -1.
Syntaxe find()
metody je:
str.find (sub (, start (, end)))
Parametry pro metodu find ()
find()
Metoda trvá maximálně tří parametrů:
- sub - Je to podřetězec, který má být prohledán v řetězci str.
- začátek a konec (volitelně) - Rozsah,
str(start:end)
ve kterém se prohledává podřetězec.
Návratová hodnota z metody find ()
find()
Metoda vrátí celé číslo:
- Pokud podřetězec existuje uvnitř řetězce, vrátí index prvního výskytu podřetězce.
- Pokud podřetězec uvnitř řetězce neexistuje, vrátí -1.
Fungování metody find ()

Příklad 1: find () S argumentem Žádný začátek a konec
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ýstup
Podřetězec „let it“: 11 Podřetězec „malý“: -1 Obsahuje podřetězec „be“
Příklad 2: find () S počátečním a koncovým argumentem
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ýstup
-1 3 -1 9