Help in string.digits functions

Discussion in 'Python' started by Anoop, Jul 25, 2006.

  1. Anoop

    Anoop Guest

    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. I have given the sample program and the output

    Thanks for ur inputs

    Anoop

    #1:
    ~~
    import string

    test='121206'

    if test not in string.digits:
    print "I am Not Digit"
    else:
    print "I am Digit"

    #2:
    ~~
    import string

    test='121206'

    if not test.isdigit():
    print "I am Not Digit"
    else:
    print "I am Digit"

    Output
    ~~~~~
    #1:I am Not Digit
    #2:I am Digit

    Thnks and Rgds

    Anoop
    Anoop, Jul 25, 2006
    #1
    1. Advertising

  2. On Mon, 2006-07-24 at 22:19 -0700, 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. I have given the sample program and the output
    >
    > Thanks for ur inputs
    >
    > Anoop
    >
    > #1:
    > ~~
    > import string
    >
    > test='121206'
    >
    > if test not in string.digits:
    > print "I am Not Digit"
    > else:
    > print "I am Digit"
    >
    > #2:
    > ~~
    > import string
    >
    > test='121206'
    >
    > if not test.isdigit():
    > print "I am Not Digit"
    > else:
    > print "I am Digit"
    >
    > Output
    > ~~~~~
    > #1:I am Not Digit
    > #2:I am Digit
    >
    > Thnks and Rgds
    >
    > Anoop
    >



    string.digits is the string constant '0123456789'

    So your test, "if test not in string.digits:" will evaluate True because
    '121206' is not in '0123456789'.

    Whereas test.isdigit() returns true if all the characters in test are
    digits.

    So yes, there is a big difference between the two.

    Regards,

    John



    --
    This message has been scanned for viruses and
    dangerous content by MailScanner, and is
    believed to be clean.
    John McMonagle, Jul 25, 2006
    #2
    1. Advertising

  3. Anoop

    John Machin Guest

    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
    John Machin, Jul 25, 2006
    #3
  4. Anoop

    Simon Forman Guest

    John McMonagle wrote:
    > On Mon, 2006-07-24 at 22:19 -0700, 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. I have given the sample program and the output
    > >
    > > Thanks for ur inputs
    > >
    > > Anoop
    > >
    > > #1:
    > > ~~
    > > import string
    > >
    > > test='121206'
    > >
    > > if test not in string.digits:
    > > print "I am Not Digit"
    > > else:
    > > print "I am Digit"
    > >
    > > #2:
    > > ~~
    > > import string
    > >
    > > test='121206'
    > >
    > > if not test.isdigit():
    > > print "I am Not Digit"
    > > else:
    > > print "I am Digit"
    > >
    > > Output
    > > ~~~~~
    > > #1:I am Not Digit
    > > #2:I am Digit
    > >
    > > Thnks and Rgds
    > >
    > > Anoop
    > >

    >
    >
    > string.digits is the string constant '0123456789'
    >
    > So your test, "if test not in string.digits:" will evaluate True because
    > '121206' is not in '0123456789'.
    >
    > Whereas test.isdigit() returns true if all the characters in test are
    > digits.
    >
    > So yes, there is a big difference between the two.
    >
    > Regards,
    >
    > John
    >
    >
    >



    Your first test could be rewritten to do what I think you're thinking
    it should do like so:

    import string

    test='121206'

    for ch in test:
    if ch not in string.digits:
    print "I am not all Digits"
    break
    else:
    print "I am all Digits"

    But isdigit() would be the better way.


    Peace,
    ~Simon
    Simon Forman, Jul 25, 2006
    #4
  5. Anoop

    Anoop Guest

    Hi All

    Thankyou verymuch for ur inputs.

    Hope u might have come across the string deprecation thought of in
    Python 3.0 as a result of which we need to use all of these as some
    objects

    For example : string.lower(str) needs to be some thing like
    str.lower().

    Can u tell me whether such a change in the common python would require
    these string.digits to be changed. If so wat would be ur suggestions

    Thanks and regards

    Anoop

    John McMonagle wrote:

    > On Mon, 2006-07-24 at 22:19 -0700, 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. I have given the sample program and the output
    > >
    > > Thanks for ur inputs
    > >
    > > Anoop
    > >
    > > #1:
    > > ~~
    > > import string
    > >
    > > test='121206'
    > >
    > > if test not in string.digits:
    > > print "I am Not Digit"
    > > else:
    > > print "I am Digit"
    > >
    > > #2:
    > > ~~
    > > import string
    > >
    > > test='121206'
    > >
    > > if not test.isdigit():
    > > print "I am Not Digit"
    > > else:
    > > print "I am Digit"
    > >
    > > Output
    > > ~~~~~
    > > #1:I am Not Digit
    > > #2:I am Digit
    > >
    > > Thnks and Rgds
    > >
    > > Anoop
    > >

    >
    >
    > string.digits is the string constant '0123456789'
    >
    > So your test, "if test not in string.digits:" will evaluate True because
    > '121206' is not in '0123456789'.
    >
    > Whereas test.isdigit() returns true if all the characters in test are
    > digits.
    >
    > So yes, there is a big difference between the two.
    >
    > Regards,
    >
    > John
    >
    >
    >
    > --
    > This message has been scanned for viruses and
    > dangerous content by MailScanner, and is
    > believed to be clean.
    Anoop, Jul 26, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,593
    Victor Bazarov
    Jun 21, 2004
  2. Anoop

    String.digits help!!!

    Anoop, Aug 8, 2006, in forum: Python
    Replies:
    5
    Views:
    499
    Ron Adam
    Aug 9, 2006
  3. zion_zii

    Obtaining digits from bit string

    zion_zii, Apr 15, 2006, in forum: C Programming
    Replies:
    5
    Views:
    329
    Thad Smith
    Apr 16, 2006
  4. loris_p
    Replies:
    3
    Views:
    358
    Roedy Green
    Jun 15, 2007
  5. Tomás Ó hÉilidhe

    String of digits, certain radix, perform division

    Tomás Ó hÉilidhe, Oct 25, 2008, in forum: C Programming
    Replies:
    5
    Views:
    403
    Thad Smith
    Oct 28, 2008
Loading...

Share This Page