If you’re anything like the vast majority of Python users, which includes us, you undoubtedly began your Python education by reading up on the print() function. You were able to compose your very own one-liner Hello, World thanks to it. You may use it to show formatted messages into the screen and perhaps locate some issues at the same time. Nevertheless, if you believe that is all there is to know about Python’s print() function, then you are losing out on a lot of great information!
Continue reading to learn how to make the most of this little-known and undervalued feature. This course will bring you up to speed on the efficient use of the print() function in Python. As you go through the parts, you should be ready for a deep dive. You may be surprised by how many useful options print() provides. You’ll be able to do the following by the time you finish this class:
-
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
Note that the print() function was a significant part of the introduction of Python 3, and that it succeeded the older print statement that was present in Python 2.
As you’ll see in a moment, there were a number of compelling factors that led to that conclusion. Although though Python 3 is the primary topic of discussion in this lesson, the previous printing method in Python has also been included for completeness.