
Your Guide to the Python print() Function
Your Guide to the Python print() Function êŽë š

Watch Now
This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:

If youâre like most Python users, including me, then you probably started your Python journey by learning about print()
. It helped you write your very own hello world
one-liner. You can use it to display formatted messages onto the screen and perhaps find some bugs. But if you think thatâs all there is to know about Pythonâs print()
function, then youâre missing out on a lot!
Keep reading to take full advantage of this seemingly boring and unappreciated little function. This tutorial will get you up to speed with using Python print()
effectively. However, prepare for a deep dive as you go through the sections. You may be surprised how much print()
has to offer!
By the end of this tutorial, youâll know how to
- Avoid common mistakes with Pythonâs
print()
- Deal with newlines, character encodings, and buffering
- Write text to files
- Mock
print()
in unit tests - Build advanced user interfaces in the terminal
If youâre a complete beginner, then youâll benefit most from reading the first part of this tutorial, which illustrates the essentials of printing in Python. Otherwise, feel free to skip that part and jump around as you see fit.
Note
print()
was a major addition to Python 3, in which it replaced the old print
statement available in Python 2. There were a number of good reasons for that, as youâll see shortly. Although this tutorial focuses on Python 3, it does show the old way of printing in Python for reference.







Conclusion
Youâre now armed with a body of knowledge about the print()
function in Python, as well as many surrounding topics. You have a deep understanding of what it is and how it works, involving all of its key elements. Numerous examples gave you insight into its evolution from Python 2. Apart from that, you learned how to:
- Avoid common mistakes with
print()
in Python - Deal with newlines, character encodings and buffering
- Write text to files
- Mock the
print()
function in unit tests - Build advanced user interfaces in the terminal
Now that you know all this, you can make interactive programs that communicate with users or produce data in popular file formats. Youâre able to quickly diagnose problems in your code and protect yourself from them. Last but not least, you know how to implement the classic snake game.
If youâre still thirsty for more information, have questions, or simply would like to share your thoughts, then feel free to reach out in the comments section below.
Quiz - The Python print() Function

In this interactive quiz, you can revisit what you know about Python's print() function. You'll also get to quiz yourself about some of its lesser-known features.
Watch Now
This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:

