Printing Variables in Python: Single, Multiple, Formatted
Four Python 3 methods to print variables with text: commas, % operator, .format(), and f-strings. Includes working code, common error fixes, and a comparison table.
Printing a variable in Python 3 requires print() with parentheses. That distinction from Python 2’s bare print statement trips up beginners every time, and knowing the four formatting methods that come after it separates readable code from string concatenation headaches.
Python’s built-in print() function accepts any number of arguments, separates them with a space by default, and ends each call with a newline. That covers most output needs in placement coding rounds.
Printing a single variable
Every Python 3 output starts the same way: call print() and pass the variable name as the argument.
# Printing a single variable in Python 3
num = 10
name = "FACE Prep"
gpa = 8.7
print(num) # Output: 10
print(name) # Output: FACE Prep
print(gpa) # Output: 8.7
Python determines the type automatically. No need to specify whether the variable is an integer, float, or string; print() handles all three identically.
Python 2 vs Python 3 syntax
Python 2 treated print as a statement, not a function. The difference in syntax is one pair of parentheses:
# Python 2 — statement (no parentheses)
num = 10
print num # Valid Python 2
# Python 3 — function (parentheses required)
num = 10
print(num) # Valid Python 3
Python 2 reached end of life on 1 January 2020 and no longer receives security or bug-fix updates. All new code targets Python 3. Write print num (no parentheses) in Python 3 and the interpreter throws SyntaxError: Missing parentheses in call to 'print' before the program even runs.
You can also print the result of an expression directly:
num = 10
print(num + 5) # Output: 15
print(num * 2) # Output: 20
Printing multiple variables
Pass multiple arguments to print() separated by commas. Python places a single space between each value by default.
# Printing multiple variables
num1, num2 = 10, 20
print(num1, num2) # Output: 10 20
name = "FACE Prep"
days = 30
print(name, days) # Output: FACE Prep 30
Mixed types with commas
Commas inside print() handle mixed types without a TypeError. Python converts each argument to its string representation before output:
count = 5
label = "questions"
score = 92.5
print(count, label, "correct out of 100, score:", score)
# Output: 5 questions correct out of 100, score: 92.5
Controlling the separator
The sep parameter overrides the default space between values:
print("2026", "05", "09", sep="-") # Output: 2026-05-09
print("A", "B", "C", sep="") # Output: ABC
print("first", "second", sep=", ") # Output: first, second
The Python basic programs collection covers eleven beginner programs that use this same print() pattern, a useful collection to work through before a placement coding round.
Formatting methods: adding text around variables
When output needs variables embedded inside a sentence, Python offers four approaches. All four appear in placement round questions, so knowing when each is appropriate matters.
Method 1: commas
The simplest approach is already covered above: separate the text and variables with commas. Python adds a space at each comma boundary:
name = "FACE Prep"
days = 20
print("I learned Python on", name, "in", days, "days")
# Output: I learned Python on FACE Prep in 20 days
Works for all types. No formatting syntax to remember. The space-between-arguments behaviour can occasionally produce unwanted double spaces if your text string already ends with a space.
Method 2: % operator
The % operator substitutes placeholders %s (string) and %d (integer) from a tuple on the right-hand side:
name = "FACE Prep"
days = 20
print("I learned Python on %s in %d days" % (name, days))
# Output: I learned Python on FACE Prep in 20 days
This style comes from C and Python 2 codebases. It works in Python 3 but is considered legacy. You’ll see it in older tutorials and legacy placement question banks; knowing how to read it is useful even if you don’t write new code with it.
Method 3: .format() method
The .format() method uses {} as positional placeholders and is available across all Python 3 versions:
name = "FACE Prep"
days = 20
print("I learned Python on {} in {} days".format(name, days))
# Output: I learned Python on FACE Prep in 20 days
Named placeholders make the order explicit:
print("I learned Python on {platform} in {n} days".format(platform="FACE Prep", n=20))
# Output: I learned Python on FACE Prep in 20 days
The .format() method is the right choice on platforms that run Python 3.0 through 3.5, where f-strings are not available. In practice, most campus placement platforms run Python 3.6 or later.
Method 4: f-strings (Python 3.6+)
f-strings were added in Python 3.6 via PEP 498 and are the current recommended method. Prefix the string with f and place variable names directly inside curly braces within the string:
name = "FACE Prep"
days = 20
print(f"I learned Python on {name} in {days} days")
# Output: I learned Python on FACE Prep in 20 days
f-strings evaluate the expression inside the curly braces at runtime. That means you can embed calculations too:
base = 100
bonus = 25
print(f"Total score: {base + bonus}")
# Output: Total score: 125
f-strings are shorter, more readable, and faster than .format() for the same result. They’re the default choice in any Python 3.6+ environment.
Common errors and fixes
SyntaxError: missing parentheses
# Wrong — Python 2 style
num = 42
print num # SyntaxError in Python 3
Fix: add the parentheses.
print(num) # Correct
TypeError: can only concatenate str (not “int”) to str
The + operator in Python does not convert types. Concatenating a string and an integer raises an error:
# Wrong
print("Days completed: " + 20) # TypeError
Three correct approaches:
# Option 1: commas (auto-converts)
print("Days completed:", 20)
# Option 2: str() conversion
print("Days completed: " + str(20))
# Option 3: f-string
days = 20
print(f"Days completed: {days}")
This TypeError pattern appears in calculator programs and comparison programs wherever numeric results are assembled into an output string. Getting it right the first time saves debugging time on a timed coding test.
NameError: name is not defined
# Wrong — variable used before assignment
print(total) # NameError: name 'total' is not defined
total = 90
Fix: assign the variable before calling print().
Which method to use
| Scenario | Recommended method |
|---|---|
| Quick output of one or more variables | Commas in print() |
| Text with variables, Python 3.6+ | f-string |
| Text with variables, Python 3.0–3.5 | .format() |
| Reading legacy code from a Python 2 codebase | % operator (read only) |
| Mixed types without type conversion | Commas in print() |
| Embedding a calculation in the output string | f-string |
The pattern that holds across all placement coding platforms: use commas when you need a quick debug print, use f-strings when the output format matters for the final answer. The % and .format() methods are for reading older code, not for writing new solutions.
Once you can format a variable into a string with an f-string, you have the same building block that every LLM API call uses: a formatted prompt string where variables slot into a template. TinkerLLM is where FACE Prep readers take that from a three-line print(f"...") script to a real API-connected project, at ₹299.
Primary sources
Frequently asked questions
How do I print a variable and text together in Python?
Use an f-string for the cleanest syntax: prefix the string with f and place the variable name inside curly braces. Commas in print() also work and handle mixed types without a TypeError.
What is the difference between Python 2 print and Python 3 print?
Python 2 uses a print statement with no parentheses. Python 3 replaces it with a print() function where parentheses are mandatory. Omitting them in Python 3 throws a SyntaxError immediately.
Can I print variables of different types together in Python?
Yes. Separate them with commas inside print() and Python handles type conversion automatically. For example, print(10, 'days') outputs '10 days' without a TypeError.
What does the sep parameter do in print()?
The sep parameter sets the separator between values. The default is a single space. Use sep='' for no space, sep=', ' for a comma-and-space, or any other string between values.
Why does Python throw TypeError when I use + with a string and number?
Python's + operator does not auto-convert types. Adding a string and an integer raises TypeError. Fix it with commas in print(), an f-string, or str() to convert the number to a string first.
Which formatting method should I use in a placement coding test?
f-strings are clearest and most commonly accepted on modern platforms. If the platform specifies Python older than 3.6, use .format() instead. The % operator is legacy and rarely required.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)