Python Output Variables
Learn how to output variables in Python using print(), string concatenation, f-strings, and sep/end arguments, with clear examples and gotchas.
Python's print() function is the primary tool for displaying variable values on the screen. This chapter explains every practical way to output variables — from the simplest single value all the way to formatted multi-variable strings — and covers the common pitfalls that trip up beginners.
This chapter covers:
- Printing a single variable
- Printing multiple variables with commas (
sepandendarguments) - Concatenating strings with
+ - Using f-strings for inline variable embedding
- Converting non-string values with
str()
Related chapters: Python Variables · Variable Names · Format Strings · Global Variables
Printing a Single Variable
Pass any variable directly to print() and Python converts it to text automatically.
Print a string variable
Hello, World!This works for every built-in type — numbers, booleans, lists, and so on:
count = 42
price = 9.99
active = True
print(count)
print(price)
print(active)42
9.99
TruePrinting Multiple Variables
Using Commas
Pass several variables to print() separated by commas. Python inserts a single space between them by default.
name = "Alice"
age = 30
print("Name:", name)
print("Age:", age)Name: Alice
Age: 30You can pass as many arguments as you like in a single print() call:
x = 10
y = 20
z = 30
print(x, y, z)10 20 30The sep Argument
Use sep to change the character that separates values (the default is a space):
first = "John"
last = "Doe"
print(first, last, sep="-")John-DoeThe end Argument
By default, print() adds a newline \n at the end. Override this with end:
print("Loading", end="...")
print("done")Loading...doneBoth values appear on the same line because the first print() does not emit a newline.
String Concatenation with +
Join string variables with the + operator to build a single output string:
name = "Alice"
greeting = "Hello, " + name + "!"
print(greeting)Hello, Alice!Important: The + operator only works between strings. If you try to concatenate a string with a number you get a TypeError:
age = 30
# print("Age: " + age) # TypeError: can only concatenate str (not "int") to str
print("Age: " + str(age)) # correct — convert age to string firstAge: 30Use str() to convert any non-string value before using +. When you use a comma instead of +, Python handles the conversion for you — but you lose control over spacing and formatting.
Outputting Variables with f-Strings
f-strings (Python 3.6+) are the most readable way to embed variables inside text. Prefix the string with f and place variable names (or any expression) inside {}:
name = "Alice"
score = 95.5
print(f"{name} scored {score:.1f} points")Alice scored 95.5 pointsThe :.1f inside the braces is a format specifier that rounds the float to one decimal place. f-strings can evaluate any Python expression, not just variable names:
x = 5
y = 2
print(f"{x} + {y} = {x + y}")
print(f"{x} / {y} = {x / y:.2f}")5 + 2 = 7
5 / 2 = 2.50For a full guide to format specifiers (alignment, padding, number bases), see Format Strings.
Arithmetic Results as Output
Python evaluates arithmetic expressions before printing them. You can pass the expression directly to print() without storing it in a variable first:
Making outputs based on calculations in Python
7
3
10
2.5
1Note that dividing two integers in Python 3 always returns a float (2.5, not 2). Use // for integer (floor) division:
print(5 // 2) # 2
print(5 % 2) # 12
1Checking a Variable's Type
Use type() inside print() to inspect what type a variable holds — useful when debugging:
x = 42
name = "Alice"
pi = 3.14
print(type(x))
print(type(name))
print(type(pi))<class 'int'>
<class 'str'>
<class 'float'>Comma vs. + — Quick Comparison
| Method | Syntax | Converts automatically | Controls spacing |
|---|---|---|---|
| Comma | print(a, b) | Yes | Via sep argument |
| Concatenation | print(a + b) | No — use str() | No separator added |
| f-string | print(f"{a} {b}") | Yes | Full control |
For most new code, prefer f-strings. They are the most readable and the least error-prone.