Metodo Python String rfind ()

Descrizione

Metodo delle stringhe Python rfind() restituisce l'ultimo indice in cui si trova la sottostringa str, o -1 se tale indice non esiste, restringendo facoltativamente la ricerca alla stringa [beg: end].

Sintassi

Di seguito è riportata la sintassi per rfind() metodo -

obj.rfind(str, beg=0 end=len(string))

Parametri

  • str - Specifica la stringa da cercare.

  • beg - Questo è l'indice iniziale, per impostazione predefinita è 0.

  • end - Questo è l'indice finale, per impostazione predefinita è uguale alla lunghezza della stringa.

Valore di ritorno

Questo metodo restituisce l'ultimo indice se trovato e -1 in caso contrario.

Esempio

L'esempio seguente mostra l'utilizzo del metodo rfind ().

#!/usr/bin/python

str1 = "this is really a string example....wow!!!";
str2 = "is";

print str1.rfind(str2)
print str1.rfind(str2, 0, 10)
print str1.rfind(str2, 10, 0)

print str1.find(str2)
print str1.find(str2, 0, 10)
print str1.find(str2, 10, 0)

Quando eseguiamo il programma sopra, produce il seguente risultato:

5
5
-1
2
2
-1