print, input and your first program

Beginner25 min

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

print writes its arguments to the screen, separated by spaces, and finishes with a newline.

print_basic.py
print("a", "b", "c")
print("second line")
Output
a b c
second line

Two 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_sep.py
print("a", "b", "c", sep="-")
print("2030", "06", "01", sep="/")
Output
a-b-c
2030/06/01

The 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_end.py
print("loading", end="...")
print("done")
print()
print("after a blank line")
Output
loading...done
after a blank line

print() 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”.

greeting.py
TARGET_YEAR = 2030
name = input("What is your name? ")
birth_year = int(input("Which year were you born? "))
age = TARGET_YEAR - birth_year
print("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? Ada
Which year were you born? 1990
Hello, Ada
In 2030 you will be 40

A few things to notice:

  • The text in quotes before input is the prompt. Put a trailing space in it so the answer does not touch the question.
  • int(...) around the second input turns the typed text into a whole number. The next section explains why that is needed.
  • TARGET_YEAR is 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:

input_is_text.py
text = "1990"
print(int(text) + 1)
print(text + "1")
Output
1991
19901

int(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)
Output
TypeError: can only concatenate str (not "int") to str

For 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"))
Output
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? ") + 1 fails with a TypeError.
  • Parentheses on print. print "hi" was Python 2 and is a SyntaxError today. print is a function and needs parentheses.
  • Expecting end= to add a space. It replaces the newline entirely. Write end=" " if you want one.
ExerciseA greeting with a birth year

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
greeting_sep.py
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? Ada
Which year were you born? 1990
Hello - Ada - 40

print converts the number to text itself, so no str() call is needed.

ExerciseFix the age program

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? 30
TypeError: can only concatenate str (not "int") to str
Show solution
next_year_fixed.py
age = int(input("Age? "))
print("Next year:", age + 1)
Age? 30
Next year: 31

input 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 a str. Convert with int() or float() before doing arithmetic.
  • Keep a trailing space in prompts so the answer reads cleanly.

Next: comments, indentation and a consistent code style.