Asking the user
The programs in the first module print the same thing every time they run. Most useful programs also take input: information the user gives while the program runs. In Python, the command input reads one line that the user types.
city = input("Where do you live? ")
print(city)
LisbonWhere do you live? Lisbon
LisbonHere is what happens, line by line:
input("Where do you live? ")shows the text in the parentheses and then waits. This text is the prompt: it tells the user what to type. The program stops right there until the user types a line and presses Enter.- What the user typed becomes the result of
input. Thecity =in front of it stores that text under the namecity, so the program can use it later. A name that holds a value like this is a variable. The next step looks at variables more closely. print(city)prints what is stored incity: the text the user typed. There are no quotes aroundcity, because the program should print the value, not the word "city".
What a run looks like
On a computer, the user types Lisbon right after the prompt, on the same line, and the screen shows it there. The examples on this site show it the same way: the Input block above is what the user types, and in the output it appears right after the prompt, followed by a line break. When you check a program here, the tests type the input for you, a different line in each test.
Notice the space at the end of the prompt, just before the closing quote. Without it, the answer sticks to the question: Where do you live?Lisbon. The space is part of the prompt, so it goes inside the quotes.
input without a prompt
The parentheses can also be empty. Then input() shows nothing at all and simply waits, so the typed line appears on a line of its own:
print("Type a word and press Enter.")
word = input()
print(word)
helloType a word and press Enter.
hello
helloThe first hello is what the user typed, and the second is what the program printed. Both work, but a prompt is usually friendlier: the user sees what to type right where they type it.