Metoda ENDSWITH () vraća True ako niz završava navedenim sufiksom. Ako nije, vraća se False.
Sintaksa endswith()
je:
str.endswith (sufiks (, početak (, kraj)))
završava s () parametrima
endswith()
Traje tri parametra:
- sufiks - niz ili skup sufiksa koje treba provjeriti
- start (nije obavezno) - početni položaj na kojem se sufiks treba provjeriti unutar niza.
- kraj (neobavezno) - Krajnji položaj u kojem se sufiks treba provjeriti unutar niza.
Povratna vrijednost od ENDWITH ()
endswith()
Metoda vraća Boolean.
- Vraća True ako nizovi završavaju navedenim sufiksom.
- Vraća False ako niz ne završava navedenim sufiksom.
Primjer 1: završava s () Bez parametara početka i kraja
text = "Python is easy to learn." result = text.endswith('to learn') # returns False print(result) result = text.endswith('to learn.') # returns True print(result) result = text.endswith('Python is easy to learn.') # returns True print(result)
Izlaz
False True True
Primjer 2: završava s () s parametrima početka i kraja
text = "Python programming is easy to learn." # start parameter: 7 # "programming is easy to learn." string is searched result = text.endswith('learn.', 7) print(result) # Both start and end is provided # start: 7, end: 26 # "programming is easy" string is searched result = text.endswith('is', 7, 26) # Returns False print(result) result = text.endswith('easy', 7, 26) # returns True print(result)
Izlaz
True False True
Dodavanje Tuple-a do ENDWith ()
Metodi endswith()
u Pythonu moguće je proslijediti suplekse korijena .
Ako niz završava bilo kojom stavkom korpice, endswith()
vraća True. Ako nije, vraća se False
Primjer 3: završava s () s Tuple sufiksom
text = "programming is easy" result = text.endswith(('programming', 'python')) # prints False print(result) result = text.endswith(('python', 'easy', 'java')) #prints True print(result) # With start and end parameter # 'programming is' string is checked result = text.endswith(('is', 'an'), 0, 14) # prints True print(result)
Izlaz
False True True
Ako trebate provjeriti započinje li niz s navedenim prefiksom, možete koristiti metodu startwith () u Pythonu.