Python Reference ยท Built-in Functions
Python print() function
Reference for Python print(): print several values, change sep and end, write to a file or stderr, flush output and format text with f-strings.
The built-in print() function converts its arguments to text with str(), joins them with a separator, adds an ending (a newline by default) and writes the result to a stream, normally standard output. It is the first function most people learn and the simplest debugging tool.
print() always returns None. It never changes the values it prints, and it accepts any number of positional arguments of any type.
print("Total:", 42) # Total: 42 print("a", "b", sep="-") # a-b print("loading", end="...") # no newline afterwards
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
print(*objects, sep=" ", end="\n", file=sys.stdout, flush=False)
| Name | Description |
|---|---|
*objects |
Zero or more values to print. Each is converted with str(). |
sep |
String placed between values. Default is one space. None means the default. |
end |
String written after the last value. Default is a newline. |
file |
Any object with a write(string) method. Default is sys.stdout. |
flush |
If True, flush the stream right away instead of waiting for buffering. |
Return value: None.
Raises: TypeError for a wrong type of sep/end (they must be strings or None) or an unknown keyword argument; any exception the stream raises when writing.
Usage notes
sepandendare keyword-only.print("a", "b", "-")prints three items; you must writesep="-".- No arguments prints just a newline, which is the usual way to add a blank line.
- Strings are printed as-is, other objects through
str(). Userepr()or the!rconversion in an f-string to see quotes and escapes when debugging. - Print many items from a list with
print(*items); withsep="\n"you get one per line. - Redirecting.
file=sys.stderrsends diagnostics to the error stream; a file object fromopen()writes to disk; anio.StringIOcaptures output in memory. - Buffering. Output to a terminal is usually line-buffered, but to a pipe or file it is block-buffered. Use
flush=Truefor progress indicators or when another process reads your output live. - Progress on one line:
print(f"\r{percent}%", end="", flush=True). - Not a logger. For real applications prefer the
loggingmodule, which adds levels, timestamps and destinations. - Python 2 is gone.
print "text"without parentheses is aSyntaxErrorin Python 3. - Encoding. Characters that the output encoding cannot represent raise
UnicodeEncodeError; this mostly affects old Windows consoles and redirected output.
Related
input(),str(),repr(),format(),open()- Lessons: Input and output, String formatting, Logging and warnings