A piece of Python code is a waste of time and resource without communicating with the real world. In this tutorial, you’ll master the humble, but powerful, Python print()
function.
Python’s built-in print()
function prints a string representation of any number of objects to the standard output. The print()
function has many advanced arguments to customize the behavior—such as the separator argument to set a custom separator string between the printed objects, the end argument to set a custom ending string, or the file argument that even allows you to print a string into a file object.
Python print() — Usage Examples
Learn by example! Here are some examples of how to use the print()
built-in function:
>>> print('hello') hello >>> print('hello world') hello world >>> print(42) 42 >>> print(1, 2, 3) 1 2 3 >>> print('hello', 'world', sep = '\n') hello world >>> print('hello', 'world', sep = '\n', end = '\n-----\n') hello world -----
So, how does the syntax formally look like?
Python print() — Syntax
You can use the print()
function with many different arguments.
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Arguments | *objects | An arbitrary number of arguments, comma-separated, to be printed. Can be an arbitrary object for which Python implicitly calls the str() method to obtain a printable string. |
sep = ' ' | Optional. The separator string that’s printed between the objects. Per default, it’s the empty space. | |
end = '\n' | Optional. The string printed after the objects. Per default, it’s the new line character so that each new printed output is in a new line. | |
file = sys.stdout | Optional. An object with a write(string) method so that Python can write the string into this object. Per default, the string is printed to the standard output given by sys.stdout . | |
flush = False | Optional. A Boolean True or False defining whether the output is flushed or buffered. Per default the output is buffered which means that it is not guaranteed that the output is written right away. Instead, multiple calls to the underlying output stream may be necessary before anything may be written. | |
Return Value | None | Returns nothing. |
Python print() — Video
Python print() — Return Value
The return value of print()
is None
.
Next, you’ll going to solve a puzzle about the print() statement in our interactive code shell.
Check out my new Python book Python One-Liners (Amazon Link).
If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!
The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).
Publisher Link: https://nostarch.com/pythononeliners
Python print() — Interactive Code Exercise
Let’s have a look at a practical exercise:
Exercise: Take a guess—what’s the output of this code snippet?
Python print() — Separator and End Arguments
There are two little-used arguments of the print()
function in Python.
- The argument
sep
indicates the separator which is printed between the objects. - The argument
end
defines what comes at the end of each line.
Consider the following example:
a = 'hello' b = 'world' print(a, b, sep=' Python ', end='!') # hello Python world!
The print()
function has several arguments you can use to format the output.
The argument sep
indicates the separator which is printed between the objects. By default sep
is empty space. In the code, you set it to ' Python '
.
The argument end
defines what comes at the end of each line. By default end
is a line break. In the code, you set it to '!'
. This means that print()
would print everything in one single line because there is no line break.
When we call print()
with the given arguments and objects a and b we get the output 'hello Python world!'
.
How to Print a Python List Beautifully
If you want to print a Python list in a more human-readable way, use the following three steps:
- Pass a list as an input to the
print()
function in Python. - Use the asterisk operator
*
in front of the list to “unpack” the list into the print function. - Use the
sep
argument to define how to separate two list elements visually.
>>> print(dir()) ['__annotations__', '__builtins__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'f', 'values'] >>> print(*dir(), sep = '\n') __annotations__ __builtins__ __doc__ __file__ __loader__ __name__ __package__ __spec__ f values
Full Article: Print a Python List Beautifully [Click & Run Code]
How to Print to a File in Python?
Every Python coder knows the print()
function. But most Python coders don’t know that the print()
function also has an optional file
argument. You can use any file object as the file
argument to print the results into the file.
hi = 'hello world!' file = 'hello.txt' # Method 2: print() function print(hi, file=open(file, 'a'))
Using the print()
function is a beautiful, short, easy-to-remember, and Pythonic way to write strings into a file!
The method is clearly the most concise one and I’d recommend it to any Python coder.
Full Tutorial: Python One-Liner: Write String to File
Summary
Python’s built-in print()
function prints a string representation of any number of objects to the standard output.
>>> print('hi') hi >>> print('hello', 'world') hello world >>> print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 1 2 3 4 5 6 7 8 9 10
The print()
function has many advanced arguments to customize the behavior—such as the separator argument to set a custom separator string between the printed objects, the end argument to set a custom ending string, or the file argument that even allows you to print a string into a file object.
>>> print(1, 2, 3, sep='---') 1---2---3 >>> print(*['Alice', 'Bob', 'Carl', 'Doris'], sep='....', end='XXXXXXXXX') Alice....Bob....Carl....DorisXXXXXXXXX
I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:
Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.