Come verificare se una stringa contiene una stringa in Python
-
inOperatore per controllare se una stringa contiene una sottostringa -
Metodo
str.find()per verificare se una stringa contiene una sottostringa -
str.index()Metodo - Soluzione per il controllo delle stringhe Conclusione
Molto spesso è necessario verificare se la stringa data contiene un substrato specifico. Elencheremo qui alcuni metodi, e poi confronteremo le prestazioni del tempo di esecuzione per selezionare il metodo più efficiente.
Prenderemo la stringa - It is a given string come la stringa data e given è la sottostringa da controllare.
in Operatore per controllare se una stringa contiene una sottostringa
in operator è l’operatore di controllo dei soci. x in y è valutato come True se x è un membro di y, o in altre parole, y contiene x.
Restituisce True se la stringa y contiene la sottostringa x.
>>> "given" in "It is a given string"
True
>>> "gaven" in "It is a given string"
False
in Prestazioni dell’operatore
import timeit
def in_method(given, sub):
return sub in given
print(min(timeit.repeat(lambda: in_method("It is a given string", "given"))))
0.2888628
Metodo str.find() per verificare se una stringa contiene una sottostringa
find è un metodo integrato di string - str.find(sub).
Restituisce l’indice più basso in str dove si trova la sottostringa sub, altrimenti restituisce -1 se sub non viene trovato.
>>> givenStr = 'It is a given string'
>>> givenStr.find('given')
8
>>> givenStr.find('gaven')
-1
Prestazioni del metodo str.find()Metodo
import timeit
def find_method(given, sub):
return given.find(sub)
print(min(timeit.repeat(lambda: find_method("It is a given string", "given"))))
0.42845349999999993
str.index()Metodo
str.index(sub) is a string built-in method that returns the lowest index in str where sub is found. It will raise ValueError when the substring sub is not found.
>>> givenStr = 'It is a given string'
>>> givenStr.index('given')
8
>>> givenStr.index('gaven')
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
givenStr.index('gaven')
ValueError: substring not found
str.index()Prestazioni del metodo
import timeit
def find_method(given, sub):
return given.find(sub)
print(min(timeit.repeat(lambda: find_method("It is a given string", "given"))))
0.457951
Soluzione per il controllo delle stringhe Conclusione
inoperatoreinè quello che si dovrebbe usare per controllare se una sottostringa esiste nella stringa data perché è il più velocestr.find()estr.index()potrebbero anche essere usati, ma non in modo ottimale a causa delle scarse prestazioni
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn FacebookArticolo correlato - Python String
- Come controllare se una stringa è vuota in un modo pythonico
- Converti una stringa in nome variabile in Python
- Come rimuovere gli spazi bianchi in una stringa in Python
- Estrai numeri da una stringa in Python
- Come convertire una stringa in datario in Python
- Come convertire una stringa in minuscola in Python 2 e 3
