Idenfity numbers in variables
Tim Chase
python.list at tim.thechases.com
Mon Oct 20 08:20:24 EDT 2008
> atom = 'C1'
>
> if '1' in atom:
> print 'kk'
>
> But, how can I do to identify in '1' all possibilities from 1-9, I
> tried:
>
> if '[1-9]', \d,...
You're reaching in the right direction (regexps), so just use
Python's "re" module:
import re
digit = re.compile(r'\d') # or "[0-9]"
for test in ["C1", "N98", "CJ", "3.141", "43+5i", ""]:
print test,
if digit.search(test):
print "has",
else:
print "doesn't have",
print "a digit in it"
The regexp can be tightened up if you want to ensure that it has
a letter in front of it, or a number of other tests but that
should be a decent start.
-tim
More information about the Python-list
mailing list