How do I find out whether a string has digit(s). For example, "Te6st1"
has digits (6 and 1).
Asked
Active
Viewed 54 times
2 Answers
0
You can use re.findall
:
>>> re.findall(r'\d+', "Te6st1")
['6', '1']
If you want them as integers, you can then call int
on the result:
>>> [int(n) for n in re.findall(r'\d+', "Te6st1")]
[6, 1]

Francisco
- 10,918
- 6
- 34
- 45
0
You can use the isdigit()
function with something like this
>>> s = "Te6st1"
>>> results = [(char,char.isdigit()) for char in s]
>>> results
[('T', False), ('e', False), ('5', True), ('s', False), ('t', False), ('1', True)]
You can also use the filter
operator to get all the digits
>>> digits = filter(lambda x:x.isdigit(), s)
>>> digits
'51'
Hope it helps

Eric
- 2,636
- 21
- 25