Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect numbers in string

Tags:

python

value = 'ad.41.bd'

if len(value) == len(value.strip({0,1,2,3,4,5,6,7,8,9})):
    # no numbers
else:
    # numbers present

There a cleaner way of detecting numbers in a string in Python?

like image 713
James Avatar asked Jul 11 '11 11:07

James


2 Answers

What about this?

import re
if not re.search('\d+', value):
    # no numbers
else:
    # numbers present
like image 83
Marcin Avatar answered Oct 13 '22 01:10

Marcin


To detect signs in the numbers, use the ? operator.

import re
if not re.search('-?\d+', value):
    # no numbers
else:
    # numbers present
like image 34
Darshan Chaudhary Avatar answered Oct 13 '22 01:10

Darshan Chaudhary