How to check if a string "is" an int?

Peter Hansen peter at engcorp.com
Wed Dec 21 08:22:02 EST 2005


Steven D'Aprano wrote:
> On Wed, 21 Dec 2005 07:36:15 -0500, Kent Johnson wrote:
>>Maybe so, but '15'.isdigit() == True:
> 
> Well I'll be a monkey's uncle.
> 
> In that case, the name is misleadingly wrong. I suppose it is not likely
> that it could be changed before Python 3?

That was my first thought too, Steven, but then I considered whether I'd 
think the same about the others: islower, isspace, istitle, isupper, 
isalnum, isalpha.

Some of those suffer from the same confusion, probably inspired by 
having written lots of C in the past, but certain "istitle" wouldn't be 
particularly useful on a single character.  isalnum and isalpha don't 
necessarily invoke the same mental awkwardness since, after all, what is 
"an alpha"?  It could just as well be read "is this string alphabetic" 
as "is this character 'an alpha'".

Given that Python doesn't have a distinct concept of "character" (but 
merely a string of length one), having those routines operate on the 
entire string is probably pretty sensible, and I'm not sure that naming 
them "isdigits()" would be helpful either since then it would feel 
awkward to use them on length-one-strings.

-Peter




More information about the Python-list mailing list