print, input and your first program
Write a program that asks a question, converts the answer to a number and prints a reply.
What you will learn
- Control output with the sep and end arguments of print
- Read a line of text with input
- Convert text to a number with int and float
On this page
A program that cannot talk to anyone is not much use. Python has two built-in functions for conversation: print sends text out and input brings text in.
If running a file from a terminal is still new, the fundamentals lesson on tools covers the shell and working directories. Here we stay with Python’s side of it.
print writes its arguments to the screen, separated by spaces, and finishes with a newline.
print("a", "b", "c")print("second line")a b csecond lineTwo keyword arguments change that behaviour. You write them by name, after the values to print. The first is sep, which is what goes between items. The default is a single space.
print("a", "b", "c", sep="-")print("2030", "06", "01", sep="/")a-b-c2030/06/01The second is end, which is what goes after the last item. The default is a newline, which is why each print starts on a new line. Setting end="..." keeps the next output on the same line.
print("loading", end="...")print("done")print()print("after a blank line")loading...done
after a blank lineprint() with nothing inside prints an empty line.
input
input(prompt) shows the prompt, waits for the user to type a line and press Enter, then returns what they typed. To keep that answer, store it under a name with =, as in name = input(...). Lesson 6 explains what a name really is. For now, read name = ... as “remember this as name”.
TARGET_YEAR = 2030
name = input("What is your name? ")birth_year = int(input("Which year were you born? "))
age = TARGET_YEAR - birth_yearprint("Hello,", name)print("In", TARGET_YEAR, "you will be", age)Run it with python3 greeting.py and type your answers. Here is a session where the user typed Ada and 1990:
What is your name? AdaWhich year were you born? 1990Hello, AdaIn 2030 you will be 40A few things to notice:
- The text in quotes before
inputis the prompt. Put a trailing space in it so the answer does not touch the question. int(...)around the secondinputturns the typed text into a whole number. The next section explains why that is needed.TARGET_YEARis in capitals because it is a constant by convention: a value that does not change while the program runs.
input always returns text
This is the single most common beginner error. Whatever the user types, input gives you a str, even if it looks like a number. Here the text "1990" stands in for what input would hand back:
text = "1990"print(int(text) + 1)print(text + "1")199119901int(text) converts the text to a whole number, so adding 1 works as arithmetic. Adding "1" to text just glues the characters together. Mixing the two directly stops the program:
print("1990" + 1)TypeError: can only concatenate str (not "int") to strFor a number with a decimal point, such as 3.5, use float(...) instead of int(...). And if the text cannot be a number at all, the conversion fails:
print(int("abc"))ValueError: invalid literal for int() with base 10: 'abc'Python has a way to catch an error like this and carry on, and you will see how to handle it later in the course. For now, type sensible answers when you test your programs.
Common pitfalls
- Forgetting
int().input("Age? ") + 1fails with aTypeError. - Parentheses on print.
print "hi"was Python 2 and is aSyntaxErrortoday.printis a function and needs parentheses. - Expecting
end=to add a space. It replaces the newline entirely. Writeend=" "if you want one.
Write a program that asks for a name and a birth year, then prints one line with the word Hello, the name and the age the person will reach in 2030, separated by - with sep. Use the structure of greeting.py.
Input: Ada, then 1990. Expected output: Hello - Ada - 40.
Show solution
TARGET_YEAR = 2030
name = input("What is your name? ")birth_year = int(input("Which year were you born? "))
print("Hello", name, TARGET_YEAR - birth_year, sep=" - ")What is your name? AdaWhich year were you born? 1990Hello - Ada - 40print converts the number to text itself, so no str() call is needed.
This program should ask for an age and print the age next year. With the input 30 it should print Next year: 31, but it crashes. Find the bug and fix it.
age = input("Age? ")print("Next year:", age + 1)Age? 30TypeError: can only concatenate str (not "int") to strShow solution
age = int(input("Age? "))print("Next year:", age + 1)Age? 30Next year: 31input returned the text "30", and text cannot be added to a number. Wrapping the call in int() converts it first.
Summary
print(..., sep=..., end=...)controls spacing and line endings.input()returns astr. Convert withint()orfloat()before doing arithmetic.- Keep a trailing space in prompts so the answer reads cleanly.
Next: comments, indentation and a consistent code style.