The print Function in Python: Displaying Data on the Screen
Displays passed arguments on the screen. It accepts one or more objects for output. Output can be controlled using optional arguments sep, end, file, and flush.
print(*objects, sep=' ', end='\n', file=None, flush=False)
objects— one or more objects to display.sep— string separator between objects. By default, this is a space.end— string added after the last object. By default, this is a newline.file— object where output will be directed. By default, this is the standard output stream (screen).flush— ifTrue, output occurs immediately. By default,False.
Calling print
To call print, use parentheses, just like any other function. Typically, the first program for every Python programmer is a call to the print function with one argument — the string "Hello, world!":
print("Hello, world!")
In Python 3, parentheses are required for print.
Multiple Objects for Output
To display multiple values, separate them with a comma. There can be as many positional arguments as needed. Each argument is automatically converted to a string using the same rules as str and displayed:
answer = 42
items = ["apple", "banana", "orange"]
print("The ultimate answer:", answer)
print("List of fruits:", items)
The ultimate answer: 42
List of fruits: ['apple', 'banana', 'orange']
sep — Separator
The sep argument defines the string used to separate passed arguments. By default, this is a space, but you can change it:
print("Hello", "world!")
print("Hello", "world!", sep="-")
Hello world!
Hello-world!
You can use multiple characters as a separator, or a newline character:
print("Hello", "world!", sep="/-/")
print("Hello", "world!", sep="\n")
Hello/-/world!
Hello
world!
Remove the separator entirely by passing an empty string to sep:
print("Hello", "world!", sep="")
Helloworld!
end — Ending String
The end argument defines the string added after all passed arguments are displayed. By default, this is a newline "\n":
print("Hello")
print("world!")
Hello
world!
For example, pass a space character as end, and two print calls will display text on one line:
print("Hello", end=" ")
print("world!")
Hello world!
If no objects are passed for output, only the end parameter is displayed:
print() # outputs a newline
print('Hello!')
Hello!
file — Output to a File
The file argument directs output not only to the screen but also to any object that supports the write method, such as a file. This is useful for saving program output for later analysis. To use this argument, open a file using the open function:
with open('output.txt', 'w') as file:
print("This message will be written to a file.", file=file)
flush — Output Buffering
The flush argument controls output buffering. By default, Python buffers output, meaning it does not immediately display text but collects it in a buffer and outputs it in chunks. This increases efficiency but can cause delays in data display. Set flush to True for immediate output, which is useful when you need to see results right away:
print("This message will be displayed immediately", flush=True)