difference between raw_input() and input()

Discussion in 'Python' started by baalu aanand, Aug 20, 2009.

  1. baalu aanand

    baalu aanand Guest

    Hi,

    I have used both raw_input() and input() for a same input value.
    But these gives different output.

    I have listed below what actually I have done

    >>> a = raw_input("===>")

    ===> 023
    >>> a

    '023'

    I have given the same value for the input() but it gives 19 as
    result
    >>> a = input("===>")

    ===> 023
    >>> a

    19

    Is there anything hide within this. Please illustrate the difference
    between raw_input() and input()

    Thanks in advance
    Baalu
     
    baalu aanand, Aug 20, 2009
    #1
    1. Advertising

  2. baalu aanand

    Chris Rebert Guest

    On Thu, Aug 20, 2009 at 3:24 AM, baalu aanand<> wrote:
    > Hi,
    >
    >     I have used both raw_input() and input() for a same input value.
    > But these gives different output.
    >
    >     I have listed below what actually I have done
    >
    >            >>> a = raw_input("===>")
    >                   ===> 023
    >            >>> a
    >                    '023'
    >
    >     I have given the same value for the input() but it gives 19 as
    > result
    >            >>> a = input("===>")
    >                   ===>  023
    >            >>> a
    >                    19
    >
    >  Is there anything hide within this. Please illustrate the difference
    > between raw_input() and input()


    input() === eval(raw_input())
    eval("023") --> int("23", 8) --> 19 [an integer, not a string]

    raw_input() /always/ returns a string.

    Never use input() in Python 2.x. In Python 3, raw_input() was renamed
    to input() because it's a better name and the old input() was hardly
    ever used (correctly).

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Aug 20, 2009
    #2
    1. Advertising

  3. On Thu, 20 Aug 2009 03:24:15 -0700, baalu aanand wrote:

    > Hi,
    >
    > I have used both raw_input() and input() for a same input value.
    > But these gives different output.
    >
    > I have listed below what actually I have done
    >
    > >>> a = raw_input("===>")

    > ===> 023
    > >>> a

    > '023'
    >
    > I have given the same value for the input() but it gives 19 as
    > result
    > >>> a = input("===>")

    > ===> 023
    > >>> a

    > 19
    >
    > Is there anything hide within this. Please illustrate the difference
    > between raw_input() and input()



    Did you look them up in the documentation?

    Did you try the interactive help?

    help(input)
    help(raw_input)



    Perhaps you could try some further experiments:


    >>> raw_input()

    hello world
    'hello world'
    >>> input()

    hello world
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 1
    hello world
    ^
    SyntaxError: unexpected EOF while parsing


    Does that give you a hint as to what is happening?

    How about this?

    >>> 07

    7
    >>> 08

    File "<stdin>", line 1
    08
    ^
    SyntaxError: invalid token
    >>> 010

    8
    >>> oct(8)

    010




    --
    Steven
     
    Steven D'Aprano, Aug 20, 2009
    #3
  4. baalu aanand

    Juraj G. Guest

    if you prefix number with zero, it will turn into octal number... I
    too wasn't aware of it... at least in python :/
    http://en.wikipedia.org/wiki/Octal
    It seems like bad practice to put zeroes before any decimal number in
    any language :)
    Juraj
     
    Juraj G., Aug 22, 2009
    #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. jakk
    Replies:
    4
    Views:
    12,218
  2. Replies:
    3
    Views:
    460
  3. Replies:
    2
    Views:
    444
    Dennis Lee Bieber
    May 10, 2011
  4. Frank
    Replies:
    7
    Views:
    153
    Dave Angel
    Apr 7, 2013
  5. Roy Smith
    Replies:
    33
    Views:
    204
    Larry Martell
    Jan 19, 2014
Loading...

Share This Page