Is there the same function in python as atoi() in C

Discussion in 'Python' started by Robbie, Aug 2, 2003.

  1. Robbie

    Robbie Guest

    Someone tell me,
    if not exist, how to write one with the same function?
    I have made a try, but there is no character type in python,
    so i failed, someone help me,
    thanks very much
    Robbie, Aug 2, 2003
    #1
    1. Advertising

  2. "Robbie" <> wrote in message
    news:bgfcfc$1313$99.com...
    > Someone tell me,
    > if not exist, how to write one with the same function?
    > I have made a try, but there is no character type in python,
    > so i failed, someone help me,
    > thanks very much


    int() should do the trick:

    >>> s = "81"
    >>> int(s)

    81


    Raymond Hettinger
    Raymond Hettinger, Aug 2, 2003
    #2
    1. Advertising

  3. Robbie

    John Machin Guest

    On Sat, 2 Aug 2003 11:51:54 +0800, "Robbie" <>
    wrote:

    >Someone tell me,
    >if not exist, how to write one with the same function?
    >I have made a try, but there is no character type in python,
    >so i failed, someone help me,
    >thanks very much
    >
    >


    General question: Have you read some of the tutorials?

    Regarding your specific problem:

    1. Think about it. A language without such a function would be utterly
    useless.

    2. There is a built-in function called int() -- look it up in the
    manual.

    3. Don't bother with the atoi() in the string module -- inspection of
    ...../Lib/string.py will tell you that it just calls int() anyway.

    4. No character type? How would you like that, signed or unsigned or
    implementation-defined? Python avoids that little chamber of horrors
    and does rather well by using strings of length 1. Here's a rough
    untested no-negative-allowed no-explicit-overflow-checking
    base-10-only atoi() ...

    def demo_atoi(astr):
    num = 0
    for c in astr:
    if '0' <= c <= '9':
    num = num * 10 + ord(c) - ord('0')
    else:
    raise ValueError('demo_atoi argument (%s) contains
    non-digit(s)' % astr)
    return num
    John Machin, Aug 2, 2003
    #3
  4. On Fri, 01 Aug 2003 20:55:42 -0700, washu <washu@127.0.0.1> wrote:

    >On Sat, 02 Aug 2003 11:51:54 +0800, Robbie wrote:
    >
    >> Someone tell me,
    >> if not exist, how to write one with the same function?
    >> I have made a try, but there is no character type in python,
    >> so i failed, someone help me,
    >> thanks very much

    >
    >I may be totally wrong but I believe there is a atoi in the strings
    >modules.

    Easy to check:

    >>> import string
    >>> help(string.atoi)

    Help on function atoi in module string:

    atoi(s, base=10)
    atoi(s [,base]) -> int

    Return the integer represented by the string s in the given
    base, which defaults to 10. The string s must consist of one
    or more digits, possibly preceded by a sign. If base is 0, it
    is chosen from the leading characters of s, 0 for octal, 0x or
    0X for hexadecimal. If base is 16, a preceding 0x or 0X is
    accepted.

    Regards,
    Bengt Richter
    Bengt Richter, Aug 2, 2003
    #4
    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. lonelyplanet999
    Replies:
    8
    Views:
    42,946
  2. Mike Chirico
    Replies:
    2
    Views:
    3,823
    Grumble
    Nov 19, 2003
  3. mark
    Replies:
    2
    Views:
    18,252
    Victor Bazarov
    Jun 14, 2004
  4. Sonia

    atoi function

    Sonia, Feb 12, 2005, in forum: C++
    Replies:
    8
    Views:
    22,084
    Sonia
    Feb 13, 2005
  5. Allan Bruce

    Macro for atoi similar function

    Allan Bruce, Jul 4, 2003, in forum: C Programming
    Replies:
    6
    Views:
    1,117
    Allan Bruce
    Jul 5, 2003
Loading...

Share This Page