Python Glossary Русский

all Python 3Input & outputfunction

print()

output

Built-in function that writes data to standard output (the screen). It takes any number of arguments, joins them with the sep parameter and ends the line with the end parameter.

Syntax

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

print runs str() over every argument for you, so print("x =", 42) needs no manual conversion, unlike "x =" + 42 which raises TypeError. It returns None, so x = print(a) stores None rather than the text — build the string with an f-string or " ".join(...) when you need it as a value.

Examples

print("Hello, World!")  # → Hello, World!
print("a", "b", "c", sep="-")  # → a-b-c
print("x =", 42, "y =", 3.14)  # → x = 42 y = 3.14
name = "Анна"; age = 25
print(f"{name} — {age} лет")  # → Анна — 25 лет
print(f"{3.14159:.2f}")  # → 3.14
print("Нет переноса", end="")
print("!")  # → Нет переноса!
import sys
print("ошибка", file=sys.stderr)  # → ошибка (в поток ошибок, а не в stdout)
print("сразу", flush=True)  # → сразу (буфер сброшен немедленно)
with open("_test.txt", "w") as f:
    print("в файл", file=f)  # на экран ничего — строка ушла в файл
import sys
print("ошибка!", file=sys.stderr)  # → ошибка! (в stderr)
print(1, 2, 3, sep="-", file=sys.stdout, flush=True)  # → 1-2-3
print(".", end="", flush=True)  # → . — без буфера и без переноса строки
with open("_gloss_test.txt", "w") as f:
    print("запись", file=f)  # на экран ничего — строка ушла в файл
import io
buf = io.StringIO()
print("в буфер", file=buf)
print(buf.getvalue(), end='')  # → в буфер

See also

Open in the full glossarydocs.python.orgReport an inaccuracy