Python print() Function

Python3 内置函数Python3 Built-in Functions


print()It is a built-in function in Python for outputting information.

print()It is one of the most commonly used functions in Python programming, used to output content to standard output (usually the console). It can output various types of data and supports rich formatting options.

Word Definitions: print 意为"打印",用于输出信息。


Basic Syntax and Parameters

Syntax Format

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

Parameter Description

  • Parameter objects:
    • Type: Any object
    • Description: The objects to be output, can be multiple, separated by commas.
  • Parameter sep(Optional):
    • Type: String
    • Description: The separator when outputting multiple objects, defaults to a space.
  • Parameter end(Optional):
    • Type: String
    • Description: The character at the end of output, defaults to a newline.
  • Parameter file(Optional):
    • Type: File object
    • Description: The output target, defaults to standard output.
  • Parameter flush(Optional):
    • Type: Boolean value
    • Description: Whether to flush the output immediately, defaults to False.

Function Description

  • Return value: No return value.

Examples

Example 1: Basic Usage

Examples

# Output string
print("Hello, World!")

# Output number
print(42)
print(3.14159)

# Output multiple objects (separated by commas)
print("My name is", "Tom")
# Output: My name is Tom

# Output variable
name = "Alice"
age = 25
print(name, "is", age, "years old")
# Output: Alice is 25 years old

Expected output:

Hello, World!
42
3.14159
My name is Tom
Alice is 25 years old

Code explanation:

  1. Can output strings, numbers, variables, and other objects.
  2. Multiple objects are separated by commas, and separated by spaces when output.

Example 2: sep and end Parameters

Examples

# Use sep to customize the separator
print("a", "b", "c", sep="-")  # Output: a-b-c
print("1", "2", "3", sep=" -> ")  # Output: 1 -> 2 -> 3

# Use end to customize the ending character
print("Hello", end=" ")
print("World")  # Output: Hello World (no newline)

# No newline and custom separator
for i in range(5):
    print(i, end=" ")
# Output: 0 1 2 3 4

Expected output:

a-b-c
1 -> 2 -> 3
Hello World
0 1 2 3 4

Code explanation:

  • sepControls the separator between objects.
  • endControls the ending character of the output.

Example 3: Output to File

Examples

# Output to file
with open("output.txt", "w") as f:
    print("Hello, File!", file=f)

# Read and verify
with open("output.txt", "r") as f:
    print(f.read())  # Output: Hello, File!

# Output to standard error
import sys
print("Error message", file=sys.stderr)

# Flush output immediately
print("Loading", end="", flush=True)
import time
time.sleep(1)
print("Done!")

Expected output:

Can output content to a file or standard error stream.

print()It is the most basic tool for debugging and outputting information.


Python3 内置函数Python3 Built-in Functions


Other Extensions