Learning Python – The print() Function

<span>print() is an essential built-in function in Python, serving as a bridge between the program and the user, clearly presenting information on the terminal or console. Whether it's simple strings, numbers, or complex variable values and data structures, </span><code><span>print() can easily handle them, displaying them in an intuitive format.</span><span>The syntax of the print() function is concise and flexible, with the following basic structure:</span>

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Parameter Description:

  1. <span>*objects</span>: Represents the content to be output, which can be multiple objects (such as strings, numbers, variables, etc.), separated by commas. <span>print()</span> will output these objects in order.

  2. <span>sep</span>: Specifies the separator between objects, defaulting to a space <span>' '</span>. For example:

    print("Hello", "World", sep="-")  # Output: Hello-World
  3. <span>end</span>: Specifies the character at the end of the output, defaulting to a newline <span>'\n'</span>. For example:

    print("World")  # Output: Hello World
  4. <span>file</span>: Specifies the target file for output, defaulting to standard output (i.e., terminal or console). Output can be redirected to a file, for example:

    with open("output.txt", "w") as f:    print("Hello World", file=f)
  5. <span>flush</span>: Controls whether to forcibly flush the output buffer, defaulting to <span>False</span>. If set to <span>True</span>, the output will be written immediately to the target without waiting for the buffer to fill.

<span><span>The print() function can meet diverse output needs, as shown in the examples below:</span></span>

1. Custom Separator (<span>sep</span>)

By default, <span>print()</span> separates multiple objects with a space. The <span>sep</span> parameter allows you to specify a different separator.

print("2025", "04", "18", sep="-")  # Output: 2025-04-18
print("Python", "is", "awesome", sep="*")  # Output: Python*is*awesome

2. Custom End Character (<span>end</span>)

By default, <span>print()</span> will move to a new line after output. The <span>end</span> parameter allows you to specify a different end character.

print("Hello", end=" ")  print("World")  # Output: Hello World
print("Loading", end="...\n")  # Output: Loading...

3. Redirect Output to a File (<span>file</span>)

Using the <span>file</span> parameter, you can write output to a file instead of displaying it on the terminal.

with open("output.txt", "w") as f:    print("This is a log message", file=f)

4. Force Flush Output Buffer (<span>flush</span>)

By default, <span>print()</span> output is first written to the buffer and displayed only when the buffer is full or the program ends. By using <span>flush=True</span>, you can force an immediate flush of the buffer.

import time
for i in range(5):    print(f"Countdown: {5-i}", end=" ", flush=True)    time.sleep(1)  # Output: Countdown: 5 Countdown: 4 Countdown: 3 Countdown: 2 Countdown: 1

5. Combining Multiple Parameters for Complex Output

You can use multiple parameters simultaneously to meet more complex needs.

print("Name", "Age", "City", sep=" | ", end="\n-----------------\n")
print("Alice", 25, "New York", sep=" | ")
print("Bob", 30, "London", sep=" | ")
# Output:
# Name | Age | City
# -----------------
# Alice | 25 | New York
# Bob | 30 | London

6. Dynamic Output Progress Bar

By combining the <span>end</span> and <span>flush</span> parameters, you can create a dynamic progress bar effect.

import time
for i in range(10):    print(f"\rProgress: [{'#' * (i+1)}{' ' * (9-i)}] {i+1}/10", end="", flush=True)    time.sleep(0.5)  # Output:
# Progress: [##########] 10/10

7. Output to Standard Error Stream (<span>file=sys.stderr</span>)

By setting the <span>file</span> parameter to <span>sys.stderr</span>, you can redirect output to the standard error stream.

import sys
print("This is an error message", file=sys.stderr)

8. Output Tabular Data

By combining <span>sep</span> and <span>end</span>, you can format the output of tabular data.

data = [    ["Alice", 25, "New York"],    ["Bob", 30, "London"],    ["Charlie", 35, "Paris"]]
for row in data:    print(f"{row[0]:<10} | {row[1]:^5} | {row[2]:>10}")
# Output:
# Alice      |  25  |   New York
# Bob        |  30  |     London
# Charlie    |  35  |      Paris

By flexibly using the <span>print()</span> parameters, developers can easily achieve formatted output, logging, dynamic effects, and more, fully showcasing the powerful functionality and flexibility of the <span>print()</span> function.

Leave a Comment