Help in string.digits functions

John Machin sjmachin at lexicon.net
Tue Jul 25 01:58:23 EDT 2006


Anoop wrote:
> Hi All
>
> I am getting two different outputs when i do an operation using
> string.digits and test.isdigit(). Is there any difference between the
> two.

Your first sentence appears to answer that ..but yes, there's quite a
difference. Have you read the manual?

> I have given the sample program and the output
>

There is a much better way to try out very small snippets of code than
putting them in a script: use the Python interactive prompt.

>>> import string
>>> string.digits
'0123456789'
>>> '0' in string.digits
True
>>> '9' in string.digits
True
>>> '90' in string.digits
False
>>> '90' in string.digits
False
>>> '123' in string.digits
True
>>> 'oo' in 'Anoop'
True
>>> '' in 'Anoop'
True
>>>

Manual:
"""
For the Unicode and string types, x in y is true if and only if x is a
substring of y. An equivalent test is y.find(x) != -1. Note, x and y
need not be the same type; consequently, u'ab' in 'abc' will return
True. Empty strings are always considered to be a substring of any
other string, so "" in "abc" will return True. Changed in version 2.3:
Previously, x was required to be a string of length 1.
"""

>>> '12345'.isdigit()
True
>>> ''.isdigit()
False
>>> 'xyz'.isdigit()
False
>>> '123xyz'.isdigit()
False
>>> '123 '.isdigit()
False
>>> ' 123'.isdigit()
False

Manual:
"""
isdigit( )
Return true if all characters in the string are digits and there is at
least one character, false otherwise. 
"""

HTH,
John




More information about the Python-list mailing list