Understanding Python's print() Function: Syntax, Parameters, and Practical Examples
This article explains the full syntax of Python's print() function, describes each parameter (value, sep, end, file, flush), and provides step‑by‑step examples showing how to output single or multiple values, control separators, line endings, redirect output to files, and force flushing.
The article begins by reviewing a basic Python quiz question about file extensions and a common beginner mistake when using print("1+1") , emphasizing the need to understand the print() function.
It then shows the interactive help view in IDLE that displays the complete signature of print(value, ..., sep='', end='\n', file=sys.stdout, flush=False) and explains each parameter:
value : one or more objects to output, separated by commas.
sep : string inserted between multiple objects (default is a single space).
end : string appended after the last object (default is a newline \n).
file : destination stream; default is sys.stdout (the screen).
flush : if True, forces the stream to flush its buffer.
All parameters are optional; using their default values requires no extra input.
Several practical examples are provided:
1. Printing a single value or variable:
<code>>print(1) # output number
1
>>print("你好,我是大陈") # output string
你好,我是大陈
>>print("2+3") # output literal string
2+3
>>a=1
>>print(a) # output variable
1</code>2. Printing multiple values with a comma separator (default space) and without a separator:
<code>>print("abc", "def") # default space
abc def
>>print("abc""def") # no separator
abcdef</code>3. Changing the separator using sep :
<code>>a=10; b=12; c=a*b
>>print(a, b, c, sep=',')
10,12,120
>>print("伯牙", "善", "鼓琴", sep="/")
伯牙/善/鼓琴</code>4. Controlling line endings with end :
<code>>print(1); print(2)
1
2
>>print(1, end="")
>>print(2)
12
>>print(1, end=",")
>>print(2)
1,2
>>print(1, end=" ")
>>print(2)
1 2</code>5. Redirecting output to a file using file :
<code>>print("伯牙善鼓琴,钟子期善听") # screen output
伯牙善鼓琴,钟子期善听
>>f = open("demo.txt", "w")
>>print('伯牙善鼓琴', file=f)
>>print('钟子期善听', file=f)
>>f.close()</code>The resulting demo.txt contains the two lines of poetry, demonstrating that changing the file parameter switches the output destination.
6. Using flush=True to force immediate display, illustrated with a loading animation that prints dots without newline and refreshes the output each half‑second:
<code>import time
print("Loading", end="")
for i in range(20):
print(".", end="", flush=True)
time.sleep(0.5)</code>Finally, the article notes that print() can also display containers such as lists, tuples, and dictionaries, and mentions that formatted output is possible but not covered in detail.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.