What you'll learn
- Read a line of text from the user with
input() - Convert that text to
intorfloatbefore doing arithmetic - Control what
print()puts between and after values withsepandend - Understand why a program that talks to a person is a conversation with two sides
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.

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}.")
Mahmud
29What 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}")
1.75
68Height in meters: Weight in kg:
BMI: 22.2sep 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)
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
- Doing arithmetic on the raw input.
input("Age: ") + 1fails with aTypeErrorbecause the input is text; convert withint()first. - Converting far from the input line. If the string travels through ten lines before becoming a number, every one of those lines can be wrong about its type.
- Forgetting that
printadds a newline. When you want two prints on one line, the first needsend="".
What's next
input()pauses the program and returns whatever the person typed, always as astr.- Convert with
int()orfloat()on the same line you read the input, and expectValueErrorfor bad text. sepandendcontrol whatprint()puts between values and at the end of the line.
Your programs can now listen as well as talk. Next you will let them decide: comparing values and choosing a path with if.