Before we continue, I need to tell you about the Python print function. We’ve been using the REPL so far, and the REPL automatically prints anything that is evaluated to the screen. A regular Python program will not do this though. For this reason, Python offers us the print function.
What is a function?
We haven’t talked about functions yet, but we will cover functions in-depth soon. For now, it suffices to know that a function is something you can call. It will do something specific for us, and the name often implies what it does. Functions often accept arguments, which they can use to do their specific job.
The Python print function
The Python print() function can print text to our screen. We can feed one or more arguments, and these arguments will be printed on the screen. Let’s try some print calls. Read the code first, then look at the output below it:
print(1)
print('Some text')
print('A tab:\t and some text')
print()
print('This is my age:', 39)Code language: Python (python)
Output:
1 Some text A tab: and some text This is my age: 39
A couple of observations that you might have caught yourself:
- Python’s print is called by entering its name followed by parentheses.
- The optional arguments are listed between the parentheses, separated by commas.
- You can print both numbers and strings. In fact, most data types in Python can be printed.
- You can mix argument types, like numbers and strings.
- In its most basic form, we can call print without any argument. In this case, it prints an empty new line.
- Special characters show up as what they are: the tab in
'A tab:\t and some text'appears as an actual tab. Python already turned\tinto a tab character when it read the string. The difference is in how strings are shown. The REPL shows a string’s representation, with quotes and escape sequences like\tand\n, so you, the programmer, can see exactly what’s in it.print()shows the string itself, the way the users of your software should see it. - When you give print more than one argument, it puts a space between them.
You can see the difference between the REPL and print side by side:
>>> 'A tab:\t and a newline:\n done'
'A tab:\t and a newline:\n done'
>>> print('A tab:\t and a newline:\n done')
A tab: and a newline:
doneCode language: Python (python)
Print several values with sep
By default, print puts a single space between the values you give it. You can change that with the sep argument (short for separator):
print('apples', 'pears', 'bananas')
print('apples', 'pears', 'bananas', sep=', ')
print('2026', '11', '10', sep='-')Code language: Python (python)
Output:
apples pears bananas apples, pears, bananas 2026-11-10
Print without a newline with end
Every call to print ends with a newline, so the next print starts on a new line. The end argument lets you pick what print writes at the end instead. Use end='' to stay on the same line:
print('Loading', end='')
print('...', end='')
print(' done!')
for i in range(5):
print(i, end=' ')
print()Code language: Python (python)
Output:
Loading... done! 0 1 2 3 4
The empty print() at the end finishes the line, so whatever you print next starts on a fresh line. The loop uses a for-loop, which you’ll learn about later in this tutorial.
Print variables with f-strings
You can mix text and variables by passing them as separate arguments, like we did with print('This is my age:', 39). Usually, an f-string is easier to read. Put an f before the string and the variables or expressions between curly braces:
name = 'Erik'
age = 39
print(f'My name is {name} and I am {age} years old.')
print(f'Next year, I will be {age + 1}.')Code language: Python (python)
Output:
My name is Erik and I am 39 years old. Next year, I will be 40.
You’ll learn more about f-strings on the page about Python strings.
Print to stderr or a file
Print writes to the screen by default, or more precisely, to standard output (stdout). With the file argument, you can send the output somewhere else. Error messages traditionally go to standard error (stderr):
import sys
print('Everything is fine')
print('Something went wrong!', file=sys.stderr)Code language: Python (python)
In a terminal, both lines appear on your screen. The difference shows when you redirect the output of your program to a file, for example with python3 myscript.py > output.txt. Only the first line ends up in the file, while the error message still shows on screen.
You can also print to a file you opened yourself:
with open('names.txt', 'w', encoding='utf-8') as f:
print('Alice', file=f)
print('Bob', file=f)Code language: Python (python)
This creates a file called names.txt with Alice and Bob on separate lines. Finally, there’s a flush argument. Python sometimes holds output in a buffer for a moment before writing it. With flush=True, the text is written right away, which can help when you print progress messages in a long-running program.
You’re learning about print this early because you’ll encounter it in almost every Python program and example on the web. Another reason is that, unlike the REPL, a regular Python program only shows data when you print it, like in the example above. Now that you know print, I can show you a lot more examples as complete programs, together with their output.