Skip to content
Theory · First program · lesson 2 of 7

The print function

Remember the python machine and its ribbon from the previous lesson: each print call prints one line on the ribbon. That line often needs several parts—a label and a number, a name and an age. That takes no second call: the print function accepts several values separated by commas and prints them separated by spaces.

Python
print("Age:", 30)  # Age: 30
print("a", "b", "c")  # a b c

The values in the parentheses are the function's arguments. The first line of the example has two:

  • "Age:" in quotes is a string, that is, text;
  • 30 without quotes is a number.

print prints a string without the quotes and a number as digits. Before printing, each value is turned into text, so strings and numbers can be mixed in one call. The space between Age: and 30 comes from print itself: the string "Age:" has none.

Quotes

A string can be written in double or single quotes: "Hi" and 'Hi' are the same string. The course examples use double quotes.

Quotes work as the borders of a string. The interpreter reads the text from the opening quote to the same kind of closing quote. So the other kind of quotes helps when the text itself contains a quote:

Python
print("It's late")  # It's late
print('Say "hi"')  # Say "hi"

Let's take both lines apart:

  1. In "It's late" the apostrophe stands inside double quotes, so it does not end the string.
  2. In 'Say "hi"' the double quotes stand inside single ones and are printed as ordinary characters.

A string ends only at the same kind of quote it started with.

The sep and end arguments

Writing sep="-" in the parentheses is a keyword argument: a name, the = sign and a value. print prints the plain arguments, and the keyword ones set how exactly to print. Here we need two of them: sep and end.

Python
print("a", "b", "c", sep="-")  # a-b-c
print("one", end=" ")
print("two")  # one two
print()
print("end")  # end

The sep argument

The sep argument sets what print puts between the values. By default it is a space, so print("a", "b", "c") prints a b c. In the first line of the example, sep="-" replaced the spaces with hyphens, and the result is a-b-c.

A string with no characters, "", is called empty. In sep it glues the values together with no space: print("a", "b", sep="") prints ab.

The end argument

The end argument sets what is printed at the end. By default it is a move to a new line. Picture the machine winding the ribbon on after each print, so the next call starts on a new line.

Let's take the second to the fifth lines of the example apart:

  1. print("one", end=" ") prints one and a space, but does not wind the ribbon on.
  2. print("two") continues the same line, and together they give one two.
  3. print() with no arguments only winds the ribbon on, that is, it prints an empty line of output.
  4. print("end") prints end after that empty line.

Order of arguments

Keyword arguments go after the plain ones, separated by commas, and in any order among themselves. For example, print("a", "b", end=".", sep="") prints ab., the same as the call with sep before end.

If you put a keyword argument before a plain one, the program does not start. The interpreter reports SyntaxError: positional argument follows keyword argument.

Common mistake

The comma between the arguments is missing:

Python
print("Age:" 30)  # ✗
TEXT
SyntaxError: invalid syntax. Perhaps you forgot a comma?

Without the comma the interpreter cannot tell the two values apart and stops at this line. The message ends with a hint about the missing comma. The fix is to add it: print("Age:", 30).

Key points

  • print prints several arguments separated by spaces: strings without quotes, numbers as digits.
  • A string is written in double or single quotes, and it ends at the same kind of quote it started with.
  • sep sets what goes between the values, and end sets what is printed at the end.
  • Keyword arguments go after the plain ones.

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way