Python quesiton
Jordan Katz
katz@underlevel.net
03 Feb 2001 01:29:13 -0500
Edward Glowacki <glowack2@msu.edu> writes:
> On Fri, 2 Feb 2001, Mark Szidik wrote:
> > Is there a simple way to test a string to see if it holds numeric
> > characters in python? I can't seem to find anything simple.
>
> *whips out the sledgehammer that is Python and prepares to hammer
> out some code...*
[snip solution w/regexps]
Here's a way to do it without using regular expressions. The code
uses string.digits, which I believe was defined for this purpose:
def with_numbers(s):
import string
for c in s:
if c in string.digits:
return 1
return 0
>>> with_numbers('hello')
0
>>> with_numbers('R2D2')
1
>>> with_numbers('_!#$%$##@')
0
Hope this helps.
--
Jordan Katz <katz@underlevel.net> | Mind the gap