Interacting with raw_input

Python also has a feature which lets you interact with the outside world to get input.  The raw_input() function waits for the user to type some input and press return.  It then gets whatever was typed.

>>> raw_input()
some input
'some input'

It’s not obvious what is going on here.  When you hit return at the end of the first line, a blank line appears.  You type what you want into the line and when you hit enter Python gets the line and (in this case, because we’re in an interactive window) prints what it got.  Do it yourself to see, because the text doesn’t show how it happens.

We might have instead assigned the input to a placeholder:

>>> a = raw_input()
some more input, where is it all going?
>>> # note it hasn't printed anything?
>>> a
'some more input, where is it all going?'

In a program this allows us to get information into the program, which might come in handy for maths (aka ‘math’) homework:

>>> a=raw_input()
>>> b=raw_input()
>>> a+b

Ooops!  The input that Python is getting is returned to us as strings.  And strings have a funny form of plus called concatenation.  We can use what we learned earlier to push this input into numbers:

>>> int(a)+int(b)

That’s what we were looking for (notice that there are no quotes around 68, because it’s a number now).

We are working in the interactive shell at the moment so the context of what is going on is clear.  However, when running a python program “in the wild” there won’t be that context.  You can add some information for the user if you put a message inside the raw_input brackets.  The message must be a string – so it must have quotes or it must be a variable which holds something which has quotes:

>>> a=raw_input('Enter your number here: ')
Enter your number here: 34

Can you see the string has been reproduced in front of the place you type?  This is called a ‘prompt’ because it prompts you to enter something.

10 Responses to Interacting with raw_input

  1. Pingback: Consolidation: CryptoPyThy « Python Tutorials for Kids 8+

  2. Casey says:


  3. John Rambo says:

    I have absolutely no idea whats going on.

  4. Joe says:

    >>> a = int(raw_input( ) )

    >>> b = int(raw_input( ) )

    >>> a + b


  5. abdullah meale says:

    is this running on python, or on python3?

  6. jontron says:

    I really, really like it. please.

  7. Stephen says:

    Nice you can see here also.

  8. John Mobey says:

    i made a pin cod with this i have been trying for ages all i needed was the int() thanks!

  9. Nithya V says:

    Nice article on raw_input. I have written an article on the same topic.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: