Steven D'Aprano wrote: > Judging by the tone of the original poster's question, I'd say for sure he > is an utter Python newbie, probably a newbie in programming in general, > so I bet that what (s)he really wants is something like this: > > somefunction("6") > -> It is a number. > > somefunction("x") > -> It is a character. > > So, at the risk of completely underestimating Tuvas' level of programming > sophistication, I'm going to answer the question I think he meant to ask: > how do I tell the difference between a digit and a non-digit? > > import string > def isDigit(s): > if len(s) != 1: > # a digit must be a single character, anything more > # or less can't be a digit > return False > else: > return s in string.digits > > > If you know that you are only dealing with a single character c, never a > longer string, you can just use the test: > > c in string.digits
Or better yet, use str.isdigit: py> '6'.isdigit() True py> 'x'.isdigit() False py> def is_single_digit(s): ... return len(s) == 1 and s.isdigit() ... py> is_single_digit('6') True py> is_single_digit('66') False STeVe -- http://mail.python.org/mailman/listinfo/python-list