How to Print Multiple Values in Python: Simple Guide
In Python, you can print multiple values by separating them with commas inside the
print() function, which adds spaces automatically. You can also use f-strings or str.format() to combine values into a single string before printing.Syntax
The basic way to print multiple values is to list them separated by commas inside the print() function. Python will print each value separated by a space by default.
Example syntax:
print(value1, value2, value3)
Here, value1, value2, and value3 can be any data types like numbers, strings, or variables.
python
print('Hello', 'world', 2024)
Output
Hello world 2024
Example
This example shows how to print multiple values using commas, f-strings, and str.format(). It demonstrates different ways to combine values in one print statement.
python
name = 'Alice' age = 30 height = 1.65 # Using commas print('Name:', name, 'Age:', age, 'Height:', height) # Using f-string (Python 3.6+) print(f'Name: {name}, Age: {age}, Height: {height}') # Using str.format() print('Name: {}, Age: {}, Height: {}'.format(name, age, height))
Output
Name: Alice Age: 30 Height: 1.65
Name: Alice, Age: 30, Height: 1.65
Name: Alice, Age: 30, Height: 1.65
Common Pitfalls
One common mistake is trying to concatenate different data types directly with + without converting them to strings, which causes errors.
Wrong way:
print('Age: ' + 30)This causes a TypeError because you can't add a string and an integer directly.
Right way:
print('Age: ' + str(30))Or better, use commas or f-strings to avoid manual conversions.
python
try: print('Age: ' + 30) except TypeError as e: print('Error:', e) print('Age:', 30) # Correct print(f'Age: {30}') # Correct
Output
Error: can only concatenate str (not "int") to str
Age: 30
Age: 30
Quick Reference
| Method | Usage | Notes |
|---|---|---|
| Commas in print | print(val1, val2, val3) | Prints values separated by spaces automatically |
| f-strings | print(f'{val1} {val2}') | Easy to read, supports expressions, Python 3.6+ |
| str.format() | print('{} {}'.format(val1, val2)) | Works in older Python versions |
| String concatenation | print(str(val1) + str(val2)) | Requires manual conversion, error-prone |
Key Takeaways
Use commas inside print() to print multiple values separated by spaces easily.
f-strings provide a clean and readable way to print multiple values with formatting.
Avoid concatenating different data types without converting them to strings first.
str.format() is a good alternative for Python versions before 3.6.
Printing multiple values with commas automatically adds spaces between them.