how to check if a string contains any numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how to check if a string contains any numbers?

3rd Sep 2020, 9:29 AM
Jeet Swarnakar
Jeet Swarnakar - avatar
4 Answers
+ 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)
3rd Sep 2020, 10:19 AM
Lothar
Lothar - avatar
+ 4
import re a = '[0-9]' print(bool(re.findall(a,your_string)))
3rd Sep 2020, 12:24 PM
madeline
madeline - avatar
+ 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.
3rd Sep 2020, 6:14 PM
Lothar
Lothar - avatar
+ 3
Lothar the code has been fixed
3rd Sep 2020, 6:30 PM
madeline
madeline - avatar