Comment vérifier qu'une chaîne de caractères contient un nombre en Python
-
Fonction Python
anyavecstr.isdigitpour vérifier si une chaîne contient un nombre -
re.search(r'\d')pour vérifier si une chaîne de caractères contient un nombre
Cet article présente la manière de vérifier si une chaîne Python contient un nombre ou non.
La fonction Python intégrée any function avec str.isdigit renverra True si la chaîne donnée contient un nombre, sinon elle renverra False.
La méthode de recherche par expression régulière en Python avec le motif r'\d' pourrait aussi retourner True si la chaîne donnée contient un nombre.
Fonction Python any avec str.isdigit pour vérifier si une chaîne contient un nombre
Toute fonction retourne True si un élément de l’alphabet donné est True, sinon, elle retourne False.
La fonction str.isdigit() retourne True si tous les caractères de la chaîne donnée sont des chiffres, False sinon.
any(chr.isdigit() for chr in stringExample)
Si stringExample contient au moins un nombre, alors le code ci-dessus retourne True parce que chr.isdigit() for chr in stringExample a au moins un True dans le iterable.
Exemple de travail
str1 = "python1"
str2 = "nonumber"
str3 = "12345"
print(any(chr.isdigit() for chr in str1))
print(any(chr.isdigit() for chr in str2))
print(any(chr.isdigit() for chr in str3))
Production:
True
False
True
Fonction map()
Python map(function, iterable) fuction applique la fonction à chaque élément de l’“iterable” donné et retourne un itérateur qui donne le résultat ci-dessus.
Par conséquent, nous pourrions réécrire la solution ci-dessus pour,
any(map(str.isdigit, stringExample))
Exemple de travail
str1 = "python1"
str2 = "nonumber"
str3 = "12345"
print(any(map(str.isdigit, str1)))
print(any(map(str.isdigit, str2)))
print(any(map(str.isdigit, str3)))
Production:
True
False
True
re.search(r'\d') pour vérifier si une chaîne de caractères contient un nombre
re.search(r'\d', string) pattern scanne la chaîne et retourne l’objet correspondant au premier emplacement qui correspond au motif donné - d cela signifie tout nombre entre 0 et 9 et retourne None si aucune correspondance n’est trouvée.
import re
print(re.search(r"\d", "python3.8"))
# output: <re.Match object; span=(6, 7), match='3'>
L’objet de correspondance contient le double “span” qui indique la position de début et de fin de la “correspondance”, ainsi que le contenu correspondant, comme “correspondance = ‘3”.
La fonction bool() peut faire passer le résultat de re.search de match object ou None à True ou False.
**Exemple de travail
import re
str1 = "python12"
str2 = "nonumber"
str3 = "12345"
print(bool(re.search(r"\d", str1)))
print(bool(re.search(r"\d", str2)))
print(bool(re.search(r"\d", str3)))
Production:
True
False
True
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 FacebookArticle connexe - Python String
- Supprimer les virgules de la chaîne en Python
- Comment vérifier si une chaîne est vide de manière pythonique
- Convertir une chaîne en nom de variable en Python
- Comment supprimer les espaces dans une chaîne de caractères en Python
- Comment extraire des nombres d'une chaîne de caractèresen Python
- Comment convertir une chaîne de caractères en datetime en Python
