How to Use the Plus Operator and Comma Separator in Python Print Statements
When displaying output in Python, you will frequently need to combine text with variables, numbers, and other data types. The print() function offers two primary ways to do this: passing multiple arguments separated by commas (,) or joining strings together with the plus operator (+).
While both approaches can produce similar-looking output, they behave very differently in how they handle data types, spacing, and errors. This guide explains how each method works, compares their behavior side by side, and shows you when to use each one, including the modern f-string alternative.
Using Commas: Automatic Conversion and Spacing
Passing multiple arguments to print() separated by commas is the most flexible approach. Python automatically converts each argument to its string representation and inserts a space between them:
name = "Alice"
score = 95
passed = True
print("Student:", name, "| Score:", score, "| Passed:", passed)
Output:
Student: Alice | Score: 95 | Passed: True
Notice that integers, floats, booleans, and any other type can be mixed freely. Python handles the conversion internally by calling each object's __str__ method before printing.
Key benefits of the comma approach:
- Accepts any data type without requiring manual conversion.
- Automatically inserts spaces between each argument.
- No risk of
TypeErrorfrom mixing strings with numbers.
When you separate arguments with commas, print() receives them as individual arguments. It converts each one to a string internally and joins them with the separator (a space by default). This is why you never need to call str() manually.
Using Plus: String Concatenation
The plus operator performs string concatenation, joining strings together into a single new string before passing it to print():
greeting = "Hello"
target = "World"
print(greeting + ", " + target + "!")
Output:
Hello, World!
Unlike commas, the plus operator does not add any automatic spacing. You control exactly what appears in the output by including spaces explicitly in your strings.
The mandatory type conversion
The plus operator only works between strings. If any operand is not a string, Python raises a TypeError:
item = "Apples"
count = 5
price = 2.99
print("Item: " + item + " | Qty: " + str(count) + " | Price: $" + str(price))
Output:
Item: Apples | Qty: 5 | Price: $2.99
Every non-string value must be wrapped in str() before concatenation.
Forgetting to convert a non-string value is one of the most common beginner errors:
age = 25
print("Age: " + age)
Output:
TypeError: can only concatenate str (not "int") to str
The fix is to explicitly convert the value:
age = 25
print("Age: " + str(age))
Output:
Age: 25
Customizing Comma Output with sep and end
The comma approach offers additional control through two parameters of the print() function: sep (separator) and end (line ending).
Changing the separator
By default, print() inserts a single space between comma-separated arguments. The sep parameter lets you change this to any string:
# Format a date with hyphens
print("205", "12", "25", sep="-")
Output:
2025-12-25
# Build a file path
print("home", "user", "documents", sep="/")
Output:
home/user/documents
Changing the line ending
By default, print() adds a newline character (\n) at the end. The end parameter lets you replace it:
print("Loading", end="... ")
print("Complete!")
Output:
Loading... Complete!
Combining sep and end
print("A", "B", "C", sep=" -> ", end=" [DONE]\n")
Output:
A -> B -> C [DONE]
Direct Comparison
Here is the same output produced with both approaches, so you can see the differences clearly:
name = "Bob"
age = 30
# Comma approach: automatic spacing and type conversion
print("Name:", name, "Age:", age)
# Plus approach: manual spacing and explicit str() calls
print("Name: " + name + " Age: " + str(age))
Output:
Name: Bob Age: 30
Name: Bob Age: 30
The output looks identical, but the comma version required no str() call for age and automatically added spaces after each argument.
Removing the automatic space from commas
If the automatic space from commas is unwanted, set sep to an empty string:
name = "Bob"
age = 30
print("Name:", name, "Age:", age, sep="")
Output:
Name:BobAge:30
This removes all automatic spacing, which is rarely what you want. For fine-grained control over spacing, f-strings (covered below) are usually a better choice.
Method Comparison
| Feature | Comma (,) | Plus (+) |
|---|---|---|
| Type handling | Automatic conversion | Strings only, manual str() required |
| Spacing | Automatic, customizable with sep | Fully manual |
| Error risk | Low | Higher (TypeError with mixed types) |
| Memory behavior | Passes references to print() | Creates a new concatenated string in memory |
| Best for | Quick debugging, logging, simple output | Building string variables for later use |
f-Strings: The Modern Alternative
For most real-world formatting needs, f-strings (formatted string literals, introduced in Python 3.6) combine the best qualities of both approaches. They handle type conversion automatically, give you full control over spacing and formatting, and produce clean, readable code:
name = "Charlie"
balance = 1234.56
active = True
print(f"User: {name} | Balance: ${balance:,.2f} | Active: {active}")
Output:
User: Charlie | Balance: $1,234.56 | Active: True
F-strings can also evaluate expressions directly inside the curly braces:
items = 5
price = 9.99
print(f"Total: ${items * price:.2f}")
Output:
Total: $49.95
- Use commas for quick debugging and simple output where automatic spacing is convenient.
- Use f-strings for formatted output in production code where you need control over layout and number formatting.
- Use plus concatenation when you need to build a string as a variable for use beyond just printing it.
Practical Examples
Quick debugging with commas
Commas are the fastest way to inspect variables during development because you never need to worry about types:
debug_data = [1, 2, 3]
print("Debug:", debug_data, "Length:", len(debug_data), "Type:", type(debug_data))
Output:
Debug: [1, 2, 3] Length: 3 Type: <class 'list'>
Formatted status messages with f-strings
f-strings produce clean, professional output for user-facing messages:
progress = 75
total = 200
completed = 150
print(f"Download progress: {completed}/{total} ({progress}% complete)")
Output:
Download progress: 150/200 (75% complete)
Building strings with plus for later use
When you need the resulting string as a variable rather than printing it immediately, concatenation makes the intent clear:
filename = "report_" + "2025" + ".pdf"
filepath = "/documents/" + filename
print(filepath)
Output:
/documents/report_2025.pdf
That said, f-strings work equally well for this purpose and are more readable when variables are involved:
year = 2025
filename = f"report_{year}.pdf"
filepath = f"/documents/{filename}"
print(filepath)
Output:
/documents/report_2024.pdf
Summary
Python gives you multiple ways to combine text and variables in print() statements, each suited to different situations:
- Commas are the simplest and safest option. They accept any data type, handle conversion automatically, and add spaces between arguments. Use them for quick debugging and simple output.
- Plus concatenation gives you precise control over the resulting string but requires all operands to be strings. Forgetting a
str()call leads to aTypeError. Use it when building strings as variables. - F-strings are the recommended approach for most formatted output. They combine automatic type conversion with full formatting control, producing readable and maintainable code.
For everyday Python development, start with f-strings as your default and fall back to commas for quick debugging. Reserve plus concatenation for cases where you specifically need to construct a string variable step by step.