+ 2
how to check if a string contains any numbers?
6 Réponses
+ 6
Here is version that uses any() function:
inp = 'this is 1 person'
print(True) if any(i.isdigit() for i in inp) else print(False)
+ 4
import re
a = '[0-9]'
print(bool(re.findall(a,your_string)))
+ 4
madeline , there seems to be an issue with the code you presented.
import re
your_string = 'f123de'
a = '[0-9]'
print(bool(re.match(a,your_string))) # missing closing parenthesis at the end of the line
The regex only find digits if they are at the beginning of the string. When the string starts with a character it returns False.
+ 3
Lothar the code has been fixed