Print and Input
Learning Objectives
- You can write a Python program that prints text to the user.
- You can use
input(...)to ask the user for a value. - You understand that
input(...)returns text, even when the user types digits. - You can explain the behavior of a short input-output program in plain language.
The first programs you write in any new language tend to do two things: they show information to the user, and they read information from the user. In Python, these are done with print(...) and input(...).
Showing Output with print
The print function displays a value to the user. The simplest case is to print a fixed piece of text:
print("Hello, world!")When this program runs, it shows Hello, world! in the output area, then ends.
You can also use several print statements:
print("Hello!")print("Welcome to the study log.")This produces two lines of output, one for each print call. Programs do exactly what you tell them to do, no more and no less. If you want three lines of output, you write three print statements.
Print output
0 / 5 points
Here is a program:
print("Hello!")print("Welcome to the study log.")What does the program print?
Asking the User with input
The input function asks the user for a value. The string inside input(...) is the prompt that the user sees, and whatever the user types and submits is the value the function gives back.
favorite_color = input("What is your favorite color? ")print(f"You chose: {favorite_color}")When this program runs:
- Python prints the prompt
What is your favorite color?and waits. - The user types a value, for example
blue, and submits it. - Python stores that value in a variable called
favorite_color. - The program prints
You chose: blue.
The prompt is a small kindness. It tells the user what the program expects from them. Without it, the program would just sit silently waiting. Try running the program in the course sandbox.
We’ll look into Variables in the next chapter, but for now you can think of
favorite_coloras a box where Python stores the user’s answer. The name of the box isfavorite_color, and the value inside it is whatever the user typed.
Reading and printing with natural-language programming
0 / 5 points
Drag and drop the natural-language programming steps to the area marked with “Program”. Arrange the steps so that the code reads a color from the user, prints “Thank you!”, and then prints the color.
Let’s look at the program for asking for input and printing the value that the user provided in a bit more detail:
favorite_color = input("What is your favorite color? ")print(f"You chose: {favorite_color}")The line 1 asks for the input and stores it in a variable.
The line 2, print(f"You chose: {favorite_color}"), shows how to insert a variable’s value into a piece of text. This is done with an f-string: a string with an f immediately before the opening quote, where anything inside {...} is replaced by the value of that variable when the line runs. So f"You chose: {favorite_color}" becomes You chose: blue when favorite_color holds "blue".
You will see f-strings everywhere in this course. Anywhere you want to show the user a value — a name, a number, a count — an f-string is how you do it. The full story of strings, including a more general way to combine them, comes in Working with Strings.
At the moment, running programs that ask for input is only possible in the course sandbox that you can open by clicking the “Python” icon in the bottom right corner of this page. Running a program that asks for input in the exercise area is not currently supported. You can write the code in the exercise area, but you will need to copy it into the sandbox to run it and see how it works.
Ask and echo
0 / 10 points
Write a program that asks the user for a snack they would like with input(...).
Store the answer in a variable, then print a response that includes the exact snack the user typed. Remember to use an f-string to include the variable in the output.
Example input:
mangoOne possible output:
You chose: mangoinput Always Gives You Text
This is a detail that matters more than it looks: input always returns text, even when the user types digits.
For example, if the user types 42, Python stores that as the text "42", not as the number 42. The two are not the same thing, and Python will treat them differently. Suppose you wanted to take whatever the user typed and add 1 to it:
answer = input("Pick a number: ")total = answer + 1print(total)This program will produce an error on the second line. The + there is meant as addition — adding the number 1 to the user’s number — but answer is text, not a number. Python will not silently mix the two.
To treat the input as a number, you need to convert it first:
answer_text = input("Pick a number: ")answer = int(answer_text)total = answer + 1print(total)You will look at conversions in more detail in the Numbers and Arithmetic chapter. For now, the important point is that input and numbers do not mix automatically.
Input gives text
0 / 5 points
A user types 7 when this line runs:
value = input("Number: ")What is stored in value?
A Short Conversation as a Program
Input-output programs are often easiest to understand if you read them as short conversations. Here is a small example:
name = input("What is your name? ")print(f"Hi {name}, nice to meet you!")In plain language:
- The program asks for the user’s name.
- The user answers with their name.
- The program responds with a personalized greeting.
When you read a piece of code, try translating it into a sentence or two of plain language. If you can do that, you understand it. If you cannot, that is a sign to slow down and look at the lines one at a time.
Explain a short conversation program
0 / 10 points
Study this program:
snack = input("What snack would you like? ")print(f"I will bring {snack} to the study break.")Explain in plain language what happens when the program runs. Mention what the user sees, what the user types, what gets stored, and what is printed.
Use a concrete example in your explanation. For example, you may say what would happen if the user typed apple.
Small Mistakes Are Common
At this stage, many errors come from very small issues:
- a missing quotation mark,
- a missing parenthesis,
- a variable name spelled one way in the assignment and another way in the print,
- adding a variable in text with
{variable}but forgetting theffrom before the string, - or text and numbers being mixed without a conversion.
These are not signs that you are bad at programming. They are signs that programming requires precision. The goal of these early chapters is not to never make these mistakes. It is to learn to notice and fix them calmly.
Fix print and input mistakes
0 / 5 points
Build a valid program that asks for a name and prints a greeting.
Only use the blocks that belong in the final program.
Check Your Understanding
- What is the difference between
print(...)andinput(...)? - What does
input(...)give you back when the user types7? - Read this program in plain language. What does it do?
city = input("What city are you in? ")print(f"The forecast for {city} is sunny.")