print()
Writes values to standard output, which is the console by default.
- Returns
None
Example
#name = "Emma"
points = 120
print("Hi,", name)
print(f"You have {points} points")Definition and usage
#The print() function converts the given values to text and writes them on one line, separated by spaces. It adds a newline character at the end, so the next call starts on a new line.
You can change the separator and the ending with the sep and end parameters. That makes it easy to print values separated by a custom character, or several calls on the same line.
The most convenient way to combine text with a variable value is an f-string: print(f"You have {points} points").
Syntax
#print(*objects, sep=' ', end='\n', file=None, flush=False)Parameters
#*objects
any values
The values to print. Each one is converted to text as if bystr().sep
str, default ' '
The text inserted between values.end
str, default '\n'
The text appended at the end. An empty stringend=""makes the nextprint()write on the same line.file
a file object
Where to write. Defaults tosys.stdout, but you can also pass an open file orsys.stderr.flush
bool, default False
WhenTrue, the text reaches the output immediately, without buffering.
More examples
#print("HTML", "CSS", "JavaScript", sep=" → ")
for i in range(3):
print(i, end=" ")
print("done")with open("log.txt", "w", encoding="utf-8") as file:
print("Program started", file=file)The text goes into the log.txt file instead of the console.
Good practices
#print()is fine to start hunting for bugs, but in bigger programs theloggingmodule works better.- Calling
print()with no arguments prints an empty line.
Related entries
#- input()Reads a line of text typed by the user in the console.
- str()Converts a value to a string.
- f-stringsStrings with inserted expression values: f"You have {points} pts".
- open()Opens a file for reading or writing and returns a file object.
- __str__() and __repr__()Methods that define an object’s text appearance: readable for the user (__str__) and technical for the programmer (__repr__).
Spotted a mistake or missing an example? Write to us.