How to Print Multiple Arguments in Python

  1. Using the print() Function in Python 3
  2. Using the print statement in Python 2
  3. Using String Formatting
  4. Conclusion
  5. FAQ
How to Print Multiple Arguments in Python

Printing multiple arguments in Python is a fundamental skill that every programmer should master. Whether you’re a seasoned developer or just starting out, understanding how to effectively print multiple values can enhance your code’s readability and functionality.

In this article, we’ll explore how to print multiple arguments in both Python 2 and Python 3. We’ll walk through various methods with clear examples, so you can choose the one that best fits your needs. By the end of this guide, you’ll have a solid grasp of printing techniques in Python, making your coding experience smoother and more efficient.

Using the print() Function in Python 3

In Python 3, the print() function is the go-to method for printing multiple arguments. Unlike Python 2, where print was a statement, Python 3 treats it as a function, allowing for more flexibility. You can pass multiple arguments to print() by separating them with commas.

Here’s a simple example:

name = "Alice"
age = 30
city = "New York"

print(name, age, city)

Output:

Alice 30 New York

In this code, we define three variables: name, age, and city. When we call print(), we pass these variables as arguments. The output displays all three values separated by spaces. The print() function automatically adds a space between each argument, making it easy to read.

Additionally, you can customize the separator using the sep parameter. For example:

print(name, age, city, sep=", ")

Output:

Alice, 30, New York

Here, we used sep=", " to change the default space to a comma and a space. This feature is particularly useful when you want to format your output in a specific way.

Overall, using the print() function in Python 3 is straightforward and versatile, allowing you to display multiple arguments in a clean and organized manner.

Using the print statement in Python 2

If you’re working with Python 2, printing multiple arguments is slightly different. In Python 2, print is treated as a statement rather than a function. However, you can still print multiple values by separating them with commas.

Consider the following example:

name = "Bob"
age = 25
city = "Los Angeles"

print name, age, city

Output:

Bob 25 Los Angeles

In this example, we define the same variables as before. When we use the print statement, we separate each variable with a comma. The output is similar to Python 3, displaying the values with spaces in between.

If you want to customize the output further, you can use a trailing comma to prevent the automatic newline that follows the print statement:

print name,
print age,
print city

Output:

Bob 25 Los Angeles

This method allows you to control the formatting more precisely, although it can be less elegant than the print() function in Python 3.

Overall, while printing in Python 2 may seem less flexible than in Python 3, it still provides the necessary functionality to display multiple arguments effectively.

Using String Formatting

Another effective way to print multiple arguments in Python is through string formatting. This method is beneficial when you want to create a more structured output. In Python 3, you can use f-strings, which offer a clean and concise way to embed expressions inside string literals.

Here’s an example of using f-strings:

name = "Charlie"
age = 28
city = "Chicago"

print(f"{name} is {age} years old and lives in {city}.")

Output:

Charlie is 28 years old and lives in Chicago.

In this code, the f before the string indicates that it’s an f-string. You can directly embed the variables within curly braces {}. This method not only improves readability but also makes it easier to format your output.

For Python 2, you can achieve similar results using the str.format() method:

name = "Diana"
age = 32
city = "Seattle"

print("{} is {} years old and lives in {}.".format(name, age, city))

Output:

Diana is 32 years old and lives in Seattle.

In this example, we use placeholders {} in the string and call the format() method to replace them with the respective variables. This approach is more versatile than basic printing, as it allows for more complex formatting options.

Using string formatting is a powerful way to print multiple arguments in Python, making your output more informative and visually appealing.

Conclusion

Printing multiple arguments in Python is an essential skill that can greatly enhance your programming efficiency. Whether you’re using Python 2 or 3, various methods are available to achieve this. From the straightforward print() function in Python 3 to the classic print statement in Python 2, as well as string formatting techniques, you have the tools to display information clearly and effectively. By mastering these methods, you can improve the readability of your code and communicate information more effectively.

FAQ

  1. How do I print multiple variables in Python 3?
    You can use the print() function and separate the variables with commas.

  2. What is the equivalent of print() in Python 2?
    In Python 2, you use the print statement without parentheses.

  3. Can I customize the separator in the print() function?
    Yes, you can use the sep parameter in the print() function to change the default space to any character.

  4. What is an f-string in Python?
    An f-string is a way to format strings using the syntax f"string {variable}", allowing for easy variable embedding.

  5. Is string formatting available in Python 2?
    Yes, you can use the str.format() method for string formatting in Python 2.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - Python Print