Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Print Multiple Arguments in Python?
Python's print() function can handle multiple arguments in various ways. Understanding these techniques helps you create clear, readable output for different data types and formatting needs.
Basic print() Function
The simplest way to print multiple values is by passing them as separate arguments to print() ?
name = "Alice"
age = 25
city = "New York"
print("Name:", name, "Age:", age, "City:", city)
print(name, age, city, sep=" | ")
Name: Alice Age: 25 City: New York Alice | 25 | New York
Using f-Strings (Recommended)
F-strings provide the most readable and efficient way to format multiple arguments ?
name = "Bob"
course = "Python"
duration = 6
print(f"Hello {name}, you're enrolled in {course} for {duration} months")
print(f"Progress: {85}% complete")
Hello Bob, you're enrolled in Python for 6 months Progress: 85% complete
Using .format() Method
The .format() method works with placeholder braces and is compatible with older Python versions ?
name = "Carol"
subject = "Data Science"
score = 92.5
print("Student: {}, Subject: {}, Score: {:.1f}%".format(name, subject, score))
print("{0} scored {2:.1f}% in {1}".format(name, subject, score))
Student: Carol, Subject: Data Science, Score: 92.5% Carol scored 92.5% in Data Science
String Concatenation
Convert non-string values and concatenate with the + operator ?
number = 42
is_valid = True
print("Number: " + str(number) + ", Valid: " + str(is_valid))
Number: 42, Valid: True
Using % Formatting
The older % formatting style still works for printing multiple arguments ?
name = "David"
years = 3
print("Hello %s, you have %d years of experience" % (name, years))
Hello David, you have 3 years of experience
Using *args and **kwargs
Handle variable numbers of arguments dynamically ?
def print_info(*args, **kwargs):
print("Positional arguments:", *args)
for key, value in kwargs.items():
print(f"{key}: {value}")
print_info("Alice", 30, city="Boston", role="Developer")
Positional arguments: Alice 30 city: Boston role: Developer
Comparison
| Method | Python Version | Readability | Performance |
|---|---|---|---|
| f-strings | 3.6+ | Excellent | Fastest |
| .format() | 2.7+ | Good | Moderate |
| % formatting | All versions | Fair | Slower |
| Concatenation | All versions | Poor | Slowest |
Conclusion
Use f-strings for modern Python applications as they offer the best readability and performance. The .format() method works well for older Python versions, while basic print() with multiple arguments is perfect for simple debugging output.
