2.6 Getting Input from the User

The built-in input function requests and obtains user input:

In [1]: name = input("What's your name? ")
What's your name? Paul

In [2]: name
Out[2]: 'Paul'

In [3]: print(name)
Paul

The snippet executes as follows:

  • First, input displays its string argument—called a prompt—to tell the user what to type and waits for the user to respond. We typed Paul (without quotes) and pressed Enter. We use bold text to distinguish the user’s input from the prompt text that input displays.

  • Function input then returns (that is, gives back) those characters as a string that the program can use. Here we assigned that string to the variable name.

Snippet [2] shows name’s value. Evaluating name displays its value in single quotes ...

Get Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and The Cloud now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.