Pour vérifier si une chaîne de caractères peut être convertie en entier sous python, on peut par exemple passer par la méthode isdigit(), illustration:
>>> s = '17'>>> type(s)<class 'str'>>>> s.isdigit()True>>> if s.isdigit():... s = int(s)...>>> type(s)<class 'int'>
autre exemple
>>> s = 'abc'>>> type(s)<class 'str'>>>> s.isdigit()False
Références
| Liens | Site |
|---|---|
| Python String isdigit() Method | tutorialspoint.com |
| Python: Check if a string represents an int, Without using Try/Except? | stackoverflow |
| Fast checking if a string can be converted to float or int in python | stackoverflow |
| Checking if a string can be converted to float in Python | stackoverflow |
