W3docs

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 (sep and end arguments)
  • 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

python— editable, runs on the server
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
True

Printing 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: 30

You 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 30

The 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-Doe

The end Argument

By default, print() adds a newline \n at the end. Override this with end:

print("Loading", end="...")
print("done")
Loading...done

Both 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 first
Age: 30

Use 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 points

The :.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.50

For 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

python— editable, runs on the server
7
3
10
2.5
1

Note 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)   # 1
2
1

Checking 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

MethodSyntaxConverts automaticallyControls spacing
Commaprint(a, b)YesVia sep argument
Concatenationprint(a + b)No — use str()No separator added
f-stringprint(f"{a} {b}")YesFull control

For most new code, prefer f-strings. They are the most readable and the least error-prone.

Practice

Practice
In Python, how can you output variables?
In Python, how can you output variables?
Was this page helpful?