To print in Python, call the built-in print() function with what you want to show: print("Hello, world") writes Hello, world followed by a newline. Several values can be passed at once, and four keyword arguments (sep, end, file, flush) control the separator, the ending, the destination and buffering.
The examples below are for Python 3. The outputs shown are example output, written out by hand to match the documented behaviour.
Print text and values
print("Hello, world")
print(42)
print(3.5)
print(True)
print([1, 2, 3])
Example output:
Hello, world
42
3.5
True
[1, 2, 3]
The Python documentation says that all non-keyword arguments are converted to strings "like str() does". So numbers, booleans, lists and any other object can be passed straight in, with no manual conversion. Calling print() with no arguments writes only the ending, which by default is a newline, so it prints a blank line.
Print several values on one line
name = "Ada"
age = 36
print("Name:", name, "Age:", age)
Example output:
Name: Ada Age: 36
Each argument is converted to text and the pieces are joined with one space. That is why you don't need to add spaces inside the strings, and why you don't need str(age). Compare this with joining by hand using +: "Age: " + age fails because a string and an integer cannot be added, while print("Age:", age) works.
Change the separator with sep
sep is the string placed between arguments. The default is a single space.
print("2026", "11", "09", sep="-")
print("a", "b", "c", sep="")
print("one", "two", sep=", ")
Example output:
2026-11-09
abc
one, two
Per the docs, sep, end, file and flush must be given as keyword arguments. Writing print("a", "b", "-") prints three values, it does not set a separator. Both sep and end must be strings, or None, which means "use the default".
Print without a newline using end
end is the string written after the last argument. The default is "\n".
print("Loading", end="")
print(".", end="")
print(".", end="")
print(" done")
Example output:
Loading.. done
The tutorial uses the same idea when it prints the lines of a file: print(line, end=''), because each line read from a file already ends with a newline and the extra one would double-space the output. Any string works as an ending, for example end=" | " to build a row of values across several calls.
Format values with f-strings
When you need more than "values separated by spaces", build the text with an f-string. Put f before the opening quote and write expressions inside braces:
import math
print(f"The value of pi is approximately {math.pi:.3f}.")
Output, from the Python tutorial:
The value of pi is approximately 3.142.
The part after the colon is a format specifier. .3f means three digits after the decimal point. An integer after the colon sets a minimum width, which is how you line up columns:
table = {"Sjoerd": 4127, "Jack": 4098, "Dcab": 7678}
for name, phone in table.items():
print(f"{name:10} ==> {phone:10d}")
Output, from the Python tutorial:
Sjoerd ==> 4127
Jack ==> 4098
Dcab ==> 7678
The older str.format() method does the same job with {} placeholders in a normal string, for example print("{} is {} years old".format(name, age)). For a longer look at the f-string syntax, see Python f-strings explained.
Print to a file or to stderr
The file argument accepts any object with a write(string) method. If it is missing or None, sys.stdout is used.
import sys
print("Something went wrong", file=sys.stderr)
with open("log.txt", "w") as log_file:
print("first line", file=log_file)
print("second line", file=log_file)
The first call goes to the error stream, which keeps diagnostics out of anything you redirect from standard output. The second writes two lines to log.txt, and the with block closes the file afterwards. Because print() converts its arguments to text, the docs warn that it cannot be used with files opened in binary mode. For those, use file.write(...) with bytes.
Flush output immediately with flush
Output buffering is usually decided by the destination. If flush=True, the stream is forcibly flushed after the call:
print("Working...", end="", flush=True)
This matters when you print a partial line (using end="") and want it to appear right away, for example a progress message before a long step. The flush argument was added in Python 3.3.
print() uses str(), not repr()
The docs for str say that str() is implicitly used when an object is written by print(). For a string, str() is the string itself, so quotes don't appear. When you need to see exactly what a value holds, print its repr():
text = "tab:\there"
print(text)
print(repr(text))
Example output (the first line contains a real tab character):
tab: here
'tab:\there'
Print repr(value) while debugging strings that might contain trailing spaces, tabs or newlines. The same distinction applies to bytes: str(b'Zoot!') returns "b'Zoot!'", as shown in the docs.
Print a list of items one per line
items = ["apple", "pear", "plum"]
for item in items:
print(item)
print(*items, sep="\n")
Both forms print the same three lines. The *items unpacks the list into separate arguments, and sep="\n" puts a newline between them. The alternative print("\n".join(items)) also works, but str.join() raises a TypeError if the iterable holds any non-string value, so a list of numbers needs converting first. print(*numbers, sep="\n") has no such restriction, since print() converts each argument itself.
Common mistakes
- Forgetting the parentheses.
printis a function in Python 3, soprint "hello"is not valid. Writeprint("hello"). - Passing sep or end positionally. They are keyword-only.
print("a", "b", ", ")prints all three strings instead of using the last as a separator. - Concatenating text and numbers with +. Use commas, an f-string, or
str()on the number. - Expecting the return value to be useful.
print()displays text, it does not return it, soresult = print("x")leavesresultasNone. To build a string, use an f-string orstr.format().
If the output doesn't appear
Check that you are running the file you edited and that the call is actually reached, for example not inside an if branch that is never true. If you print a partial line with end="" and see nothing until the program finishes, add flush=True. If output goes to the wrong place, check whether you passed file= or are redirecting standard output when you launch the script.
Frequently asked questions
- How do I print without a newline in Python?
- Pass an empty string as the end argument: print('text', end=''). By default print() writes a newline after its output, and end replaces that newline with whatever string you give it.
- How do I print multiple values on one line in Python?
- Give print() several arguments separated by commas, for example print('x =', 5). It converts each one to a string and joins them with a single space. Use the sep argument to change the separator.
- Why does print() need parentheses in Python 3?
- In Python 3, print is a built-in function, and calling a function requires parentheses. Code written as a statement without parentheses was Python 2 style and has to be converted to print(...).
- Can print() write to a file?
- Yes. Pass any object with a write(string) method as the file argument, such as a file opened in text mode. It cannot write to a file opened in binary mode, because print() converts everything to text first.
- What is the difference between print(x) and print(repr(x))?
- print(x) shows str(x), the readable form. print(repr(x)) shows the repr, which for a string includes the quotes, so it is useful when you need to see exactly what a value contains.