Python Output
Showing information on the screen is the first thing most programs do. In Python you use the built-in print() function. It looks simple, but it has a few handy options worth knowing.
Printing values
print() accepts any value — text, numbers, lists, even the result of a calculation:
print("Good morning")
print(42)
print(3.75)
print(10 * 4)
print([1, 2, 3])Good morning
42
3.75
40
[1, 2, 3]
Calling print() with nothing inside prints an empty line.
Several values at once
Separate values with commas. print() puts a single space between them automatically and converts each one to text for you:
name = "Mai"
age = 27
print("Name:", name, "Age:", age)Name: Mai Age: 27
Changing the separator with sep
The sep keyword argument controls what goes between the values:
print("2025", "03", "14", sep="-")
print("a", "b", "c", sep="")
print("red", "green", "blue", sep=" | ")2025-03-14
abc
red | green | blue
Changing the line ending with end
By default each print() finishes with a newline character \n. Change it with end to keep printing on the same line:
print("Loading", end="")
print(".", end="")
print(".", end="")
print(". done!")
for i in range(1, 6):
print(i, end=" ")
print()Loading... done!
1 2 3 4 5
The final empty print() simply moves to a new line after the loop.
Special characters
Inside a string, a backslash starts an escape sequence:
| Escape | Meaning |
|---|---|
\n |
New line |
\t |
Tab |
\\ |
A literal backslash |
\" / \' |
A quote inside a string that uses the same quote |
print("Line one\nLine two")
print("Name\tScore")
print("She said \"hi\"")
print('It\'s fine')
print("C:\\Users\\demo")Line one
Line two
Name Score
She said "hi"
It's fine
C:\Users\demo
Combining text and numbers
You cannot add a string and a number with +:
print("Total: " + 5) # TypeError: can only concatenate str (not "int") to strUse commas, convert with str(), or — the modern favourite — use an f-string (covered in detail in String Formatting):
items = 3
price = 4.5
print("Items:", items)
print("Items: " + str(items))
print(f"{items} items cost {items * price} dollars")Items: 3
Items: 3
3 items cost 13.5 dollars
Tip: f-strings are usually the clearest way to build output that mixes text and values.
Test yourself
What does print("a", "b", sep="-") output?
- a b
- ab
- a-b
- a - b
Which argument stops print() from moving to a new line?
- sep=""
- end=""
- newline=False
- flush=True
What does the escape sequence \t produce?
- A tab
- The letter t
- A new line
- A backslash