We have string.isdigit(), why not string.isNumber()?

Discussion in 'Python' started by MooMaster, May 1, 2008.

  1. MooMaster

    MooMaster Guest

    N00b question alert! I did a search for isdigit() in the group
    discussion, and it didn't look like the question had been asked in the
    first 2 pages, so sorry if it was...

    The manual documentation says:
    "isdigit( )

    Return true if all characters in the string are digits and there is at
    least one character, false otherwise.
    For 8-bit strings, this method is locale-dependent. "

    So it makes sense that something like 5.6 would return false. But what
    if we want to make sure that our string is a valid number, ie decimals
    included?

    I know how to write a regexp or method or whatever to do this, my main
    question is *why* something like an isNumber() method is not baked
    into the class. Does such functionality exist somewhere else in the
    standard library that I'm just missing?
     
    MooMaster, May 1, 2008
    #1
    1. Advertising

  2. MooMaster

    Roy Smith Guest

    In article
    <>,
    MooMaster <> wrote:

    > So it makes sense that something like 5.6 would return false. But what
    > if we want to make sure that our string is a valid number, ie decimals
    > included?


    Just call int(x) or float(x) inside a try block and see if if it raises an
    exception.
     
    Roy Smith, May 1, 2008
    #2
    1. Advertising

  3. MooMaster

    Dan Bishop Guest

    On Apr 30, 7:56 pm, MooMaster <> wrote:
    > N00b question alert! I did a search for isdigit() in the group
    > discussion, and it didn't look like the question had been asked in the
    > first 2 pages, so sorry if it was...
    >
    > The manual documentation says:
    > "isdigit( )
    >
    > Return true if all characters in the string are digits and there is at
    > least one character, false otherwise.
    > For 8-bit strings, this method is locale-dependent. "
    >
    > So it makes sense that something like 5.6 would return false. But what
    > if we want to make sure that our string is a valid number, ie decimals
    > included?
    >
    > I know how to write a regexp or method or whatever to do this, my main
    > question is *why* something like an isNumber() method is not baked
    > into the class. Does such functionality exist somewhere else in the
    > standard library that I'm just missing?


    A string s is a valid number if float(s) does not raise a ValueError.
     
    Dan Bishop, May 1, 2008
    #3
    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. Rob Hunter

    isNumber? check

    Rob Hunter, Sep 29, 2003, in forum: Python
    Replies:
    4
    Views:
    16,060
    Miki Tebeka
    Sep 30, 2003
  2. Rob Hunter

    Re: isNumber? check

    Rob Hunter, Sep 29, 2003, in forum: Python
    Replies:
    2
    Views:
    448
  3. Rob Hunter

    Re: isNumber? check

    Rob Hunter, Sep 29, 2003, in forum: Python
    Replies:
    1
    Views:
    376
    Bengt Richter
    Sep 30, 2003
  4. Tony Meyer

    RE: isNumber? check

    Tony Meyer, Sep 30, 2003, in forum: Python
    Replies:
    1
    Views:
    331
    Peter Otten
    Sep 30, 2003
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,256
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page