The most efficient and easy way to print single and multiple variables, use the f-strings or print() method. If your Python version is older, like below 2.7, use the “%” approach.
Printing a single variable
To quickly print a single variable, use the “print()” function and pass your input to it. For example, if I have a variable x whose value is 5, then using print(x), I will be able to print “5”.
It automatically converts objects to strings (via __str__). This is the simplest approach for quick debugging. However, you have no control over formatting.
my_variable = "Hello AppDividend" num_variable = 13 print(my_variable) print(num_variable) # Output: Hello AppDividend # Output: 13
Printing multiple variables
When you want to print more than one variable, you have to format strings as well. Printing and formatting are two sides of one coin.
Here are five ways:
- Using f-strings (Most efficient approach)
- Using print()
- Using format()
- Using % formatting (Legacy approach)
- Using concatenation (less common for mixed data types)
Method 1: Using f-strings
The f-strings use the “{}” placeholders that will be replaced with the variables when the string is formatted.
It is the most efficient, concise, easy, and readable method to print multiple variables with string formatting compared to the .format() method.
The main advantage of the f-strings is that it can handle data types like integers, floats, and strings without manual conversion, and it is extremely readable inline expressions. However, f-strings are not available below Python version 3.6.
You can also use f-strings for advanced formatting (e.g., f”{value:.2f}” to round floats). Execution speed is arguably the fastest. You can mix variables with explanatory text.
my_variable = "Hello AppDividend" num_variable = 13 # Using an f-string print(f"My first variable is: {my_variable}, and my second variable is: {num_variable}") # Output: My first variable is: Hello AppDividend, and my second variable is: 13
Method 2: Using print()
Using the print() function, we can separate each variable by comma (“,”) and print variables separated by a space. The default separator is space, but you can define your custom separator using the “sep” argument.
my_variable = "Hello AppDividend" num_variable = 13 # Separated by a space (default behavior) print(my_variable, num_variable) # Separated by a comma print(my_variable, num_variable, sep=',') # Output: Hello AppDividend 13 # Output: Hello AppDividend,13
It automatically adds space between elements and no type conversion is needed. However, it can produce unwanted spaces in precise outputs. And provides limited formatting control.
You can remove trailing space with the end=”” parameter.
Method 3: Using format()
The string.format() method formats strings, especially when you want to insert variable values into a string template.
my_variable = "Hello AppDividend" num_variable = 13 print("My first variable is: {}, and my second variable is: {}".format( my_variable, num_variable)) # Output: My first variable is: Hello AppDividend, and my second variable is: 13
The .format() method is compatible with Python 2.7+ and 3.x. It supports positional and keyword arguments. However, if you have lots of placeholders, it becomes less readable.
Method 4: Using % formatting
The % formatting method is an older method similar to C-style formatting. You can use it to print multiple variables, which is helpful if you use an older version of Python, like 2.0.
my_variable = "Hello AppDividend" num_variable = 13 print("My first variable is: %s, and my second variable is: %d" % (my_variable, num_variable)) # Output: My first variable is: Hello AppDividend, and my second variable is: 13
In the above code example, %s is a placeholder for a string, and %d is a placeholder for an integer. Using those placeholders, we printed a string with multiple variables.
This approach is not recommended because it is outdated and hardly used by developers.
It is helpul when you are working with teams accustomed to C-style formatting.
Method 5: Using concatenation (+)
The concatenation (+) approach is less common because it can become complicated for mixed data types. Still, you can use it to print multiple variables.
name = "Krunal" age = 32 print("Name: " + name + ", Age: " + str(age)) # Output: Name: Krunal, Age: 32
The main disadvantage of using the + operator is that you have to do manual type conversion (str()). It becomes inefficient for large strings.
That’s all!
University of American Samoa Press
One of the very few useful python tutorials out there!