Skip to content

Input and Output

A useful program typically processes some input and produces output. Many programs manipulate text as both input and output.

print()

In Python, the built-in print() function can be used to print text to the screen.

>>> print('Hello World')
Hello World
>>> print(5)
5

You can pass more than one value to the print() function; we call these arguments. When you do so, print() will output them with a space between them.

>>> print("x", "y", "z")
x y z
>>> print("total:", 3 + 4)
total: 7

Two useful keyword options:

>>> print("a", "b", sep="-")        # (sep = what goes between arguments)
a-b
>>> print("no newline", end="")     # won't print a newline
no newline

input()

The built-in input() function is used to collect text typed by the user into a terminal and makes it available for the program to use. The input ends when the user presses ENTER.

>>> input('Please enter your name: ')
Please enter your name: Ada
'Ada'

You will always want to store the input into a variable before you can use it.

>>> name = input('Please enter your name: ')
Please enter your name: Ada
>>> print(name)
Ada

How Python runs your program

A program is a list of statements the Python interpreter carries out in order, one after another:

print("first")
print("second")
print("third")
first
second
third

That top-to-bottom flow is the default and can only be changed by flow control statements and errors.

Comments

Anything after # on a line is a comment. Python skips it entirely; it's there to explain why:

# Convert the price to cents to avoid rounding problems.
price_cents = 1999        # $19.99 stored as an integer number of cents

Good comments explain intent, not the obvious.

  • x = x + 1 # add one to x: not a very useful comment
  • x = x + 1 # advance to the next customer: this carries actual information

Strings and numbers are different kinds of thing

"42" (with quotes) is text; 42 (no quotes) is a number. They behave differently

>>> print(2 + 3)
5
>>> print("2" + "3")
23

Mixing them is an error, and a very common one:

>>> print("age: " + 30)
...
TypeError: can only concatenate str (not "int") to str
>>> print("age: ", 30)
age: 30

Reading error messages

When Python can't run something, it stops and prints a traceback. A traceback tells you what happened and where.

Always start reading from the bottom. That's where the most instructive information is.

# boom.py

print("start")
print(10 / 0)       # boom 
print("never runs")
start
Traceback (most recent call last):
  File "boom.py", line 4, in <module>
ZeroDivisionError: division by zero

  • ZeroDivisionError: division by zerowhat went wrong.
  • File "boom.py", line 4where.
  • start printed, never runs did not — Python stopped at the error.

Here are some of the most common errors:

Error Means Typical cause
SyntaxError Python can't even parse the line missing ), missing quote
NameError a name isn't defined typo, or used before assigned
TypeError wrong kind of value "age" + 30

Worked examples

# greet.py — a first real script
print("What this program does: greets the world three ways.")
print("Hello, world!")
print("Hello,", "world!")            # print joins with a space
print("Hel" + "lo" + ", world!")     # strings joined by hand
print("=" * 20)                      # ⇒ ==================== (repeat a string)
What this program does: greets the world three ways.
Hello, world!
Hello, world!
Hello, world!
====================