What Is print() in Python?
print() is a built-in function that writes values to the standard output (your screen or terminal). Its full signature is:
print(*objects, sep=” “, end=”\n”, file=sys.stdout, flush=False)
The simplest use displays a string literal. Single and double quotes work the same way:
print(“Hello”)
print(‘Hello’)
# Output:
# Hello
# Hello
How to Print a Variable in Python
character:
x = “awesome”
print(“Python is ” + x) # Python is awesome
You cannot use + to join a string and a number. print(5 + “John”) raises TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’.
The easier way is to pass values separated by commas; print() converts each to text and adds a space between them:
name = “Asha”
age = 22
print(“Name:”, name, “Age:”, age) # Name: Asha Age: 22
How Do You Format Output with format() and f-strings?
The format() method inserts values into {} placeholders. Index numbers such as {0} control which argument goes where:
quantity = 3
itemno = 567
price = 49.95
myorder = “I want to pay {2} dollars for {0} pieces of item {1}.”
print(myorder.format(quantity, itemno, price))
An f-string (formatted string literal, Python 3.6+) is a string prefixed with f. Expressions inside {} are evaluated and inserted directly, which makes f-strings the shortest and most readable option. The “f” in f”…” stands for “formatted”.
name = “Asha”
price = 49.955
print(f”Hello {name}, total = {price:.2f}”) # Hello Asha, total = 49.96
How to Print in the Same Line in Python (the end Parameter)
By default print() ends with a newline (end=”\n”). Change end to keep the next output on the same line:
for i in range(1, 4):
print(i, end=” “)
# Output: 1 2 3
The end parameter sets what is printed after the last value. end=”” prints nothing, end=”, ” adds a comma, and the default adds a line break.
How to Print Vertically in Python (the sep Parameter)
To print values one below another, set sep=”\n”, or loop over the values:
print(“a”, “b”, “c”, sep=”\n”)
for ch in “PY”:
print(ch)
# Both print each item on its own line
How to Print Formatted Output in Python: A Receipt Example
Part 2 — Tutorial
In this tutorial you will print a small shopping receipt using plain print(), comma-separated values, an f-string with 2-decimal formatting, and the end and sep parameters. It takes about 10 minutes and 5 steps.
Prerequisites
- Python 3 installed and able to run a .py
- Know how to create a variable (x = 5).
Steps
Step 1: Print a heading
Start with a simple string.
print(“=== ENTRI STORE ===”)
Step 2: Store values in variables
Keep the data separate from the printing.
item = “Notebook”
qty = 3
price = 49.5
Step 3: Print values with commas
Commas insert a space and convert numbers automatically, so no TypeError.
print(“Item:”, item, “| Qty:”, qty)
Step 4: Format the total with an f-string
Use :.2f to show exactly two decimal places.
total = qty * price
print(f”Total: Rs. {total:.2f}”) # Total: Rs. 148.50
Step 5: Use end and sep for layout
Print a divider on one line and a vertical thank-you message.
for _ in range(20):
print(“-“, end=””)
print()