Fundamentals · Lesson 8 of 94

Input and output

Watch this lesson

View full playlist ↗

Read the lesson

What you'll learn

Concept

Until now your programs only talked. input() lets them listen. It prints a prompt, waits for the person to type a line and press Enter, and hands that line back as a string. The program pauses until the line arrives, which is the first time your code depends on someone outside it.

Input and output: Diagram 1
Input and output · Figure 1 · Open for full size

Everything input() returns is a str, even if the person typed digits. "29" is text, and "29" + 1 is an error. To do arithmetic you convert with int() or float(), and you should do that immediately, at the line where the input arrives, so the rest of your program never has to wonder what type it is holding.

Conversion can fail. If someone types twenty into a prompt that expects a number, int("twenty") raises a ValueError. For now, you will let that error happen and read it; in the lesson on exceptions you will learn to catch it and ask again.

You type input() returns int() gives float() gives
29 "29" 29 29.0
1.75 "1.75" ValueError 1.75
hello "hello" ValueError ValueError

print() has two settings worth knowing. sep is what goes between the values you pass, a space by default. end is what goes after the last one, a newline by default. Setting end="" keeps the next print on the same line, which is how progress messages and prompts are built.

Code

A conversation with two questions. The stdin block that follows the code is what the person types at each prompt; the runner feeds it in, so the output is real.

# greet.py
name = input("What is your name? ")
age_text = input("How old are you? ")
age = int(age_text)

print(f"Hello, {name}. Next year you will be {age + 1}.")
Input
Mahmud
29
Output
What is your name? How old are you? Hello, Mahmud. Next year you will be 30.

Notice both prompts appear on one line: piped input is not echoed and input() does not add a newline after the prompt. When a person types at a keyboard, their Enter key supplies the line breaks. Converting on the same line as the input keeps the type clear from then on.

# convert_now.py
height = float(input("Height in meters: "))
weight = float(input("Weight in kg: "))
bmi = weight / height ** 2

print()
print(f"BMI: {bmi:.1f}")
Input
1.75
68
Output
Height in meters: Weight in kg: 
BMI: 22.2

sep and end shape the output itself. The first print joins with commas, the second builds one line from several calls, and the third draws a rule from a repeated string.

# print_options.py
print("red", "green", "blue", sep=", ")
print("Loading", end="")
print("...", end="")
print(" done")
print("-" * 20)
Output
red, green, blue
Loading... done
--------------------

Try it yourself

Create tip_calc.py. Ask for the bill amount and the tip percentage, convert both to float on the same line as each input(), and print the tip and the total with two decimals each. Run it, then run it again and type abc for the bill to see the ValueError you will learn to handle later.

Common mistakes

What's next

Your programs can now listen as well as talk. Next you will let them decide: comparing values and choosing a path with if.

Course overview