Is it possible to do something like this (using python 3):
le = "\n" var1 = 2 var2 = 5 print("INFORMATION"+le "-----------"+le "Variable1: {}".format(var1)+le "Variable2: {}".format(var2)+le )
tjati
5,5513 gold badges37 silver badges56 bronze badges
asked Jun 26, 2014 at 9:14
Yes. take a look at docs.
General example:
var1 = 2 var2 = 5 print("INFORMATION", "------------", "Variable1: {}".format(var1), "Variable2: {}".format(var2), sep='\n')answered Jun 26, 2014 at 9:26
Shadow9043Shadow9043
1,99014 silver badges12 bronze badges
You could store this string in an object and then print it:
out = "INFORMATION"+le+"-----------"+le+"Variable1: {}".format(var1)+le+"Variable2: {}".format(var2)+le print(out)Or if you want to do it easier you can also do like that:
print("INFORMATION\n-----------\nVariable1: {}\nVariable2: {}\n".format(var1, var2))Or if it is too long and you want to spare it in different lines in your code:
out = "Information\n" / "-----------\n"answered Jun 26, 2014 at 9:26
llrsllrs
3,25234 silver badges64 bronze badges
print("INFORMATION\n" "-----------\n" "Variable1: {}\n".format(var1), "Variable2: {}".format(var2) ) In [24]: print("INFORMATION"'\n' ....: "-----------"'\n' ....: "Variable1: {}\n".format(var1), ....: "Variable2: {}".format(var2) ....: ) INFORMATION ----------- Variable1: 2 Variable2: 5 print("INFORMATION", "-----------", "Variable1: {}".format(var1), "Variable2: {}".format(var2), sep='\n') In [30]: print("INFORMATION", ....: "-----------", ....: "Variable1: {}".format(var1), ....: "Variable2: {}".format(var2), ....: sep='\n') INFORMATION ----------- Variable1: 2 Variable2: 5
answered Jun 26, 2014 at 9:25
Home » Python
Python | Print multiple variables: In this tutorial, we are going to learn the concept of printing the values of multiple variables with the examples.
Submitted by IncludeHelp, on June 14, 2020
Like other programming languages, In python also, we can define and print the multiple variables. Here, we see how can we print the single and multiple variables using the print() function?
In Python, single variable can be printed like this,
print(variable)Example:
# Python program to print single variable name = "Mike" age = 21 country = "USA" # printing variables one by one print(name) print(age) print(country) print() # prints a newline # printing variables one by one # with messages print("Name:", name) print("Age:", age) print("Country:", country)Output:
Mike 21 USA Name: Mike Age: 21 Country: USAPrinting multiple variables
There are following methods to print multiple variables,
- Method 1: Passing multiple variables as arguments separating them by commas
- Method 2: Using format() method with curly braces ({})
- Method 3: Using format() method with numbers in curly braces ({0})
- Method 4: Using format() method with explicit name in curly braces ({v})
- Method 5: Using string concatenation
Let's understand each method in the details.
Method 1:
To print multiple variables using the print() function, we need to provide the variable names as arguments separated by the commas.
Note: print() function prints space after the value of each variable, space is the default value of sep parameter – which is an optional parameter in print() function, by using this parameter, we can specify the separator value.
Syntax:
print(variable1, varaible2, variable3, ...)Example:
# Python program to print multiple variables name = "Mike" age = 21 country = "USA" # printing variables one by one print("Printing normally...") print(name, age, country) print() # prints a new line # Printing with comma seprator print("Printing with comma seprator...") print(name, age, country, sep=',') print() # prints a new line # printing variables with messages print("Printing with messages...") print("Name:", name, "Age:", age, "Country:", country)Output:
Printing normally... Mike 21 USA Printing with comma seprator... Mike,21,USA Printing with messages... Name: Mike Age: 21 Country: USAMethod 2:
By using the new-style string formatting (format() method), we can also print the multiple variables. Here, we have to specify the curly braces ({}) where we have to print the values and in the format() method, provide the multiple variables separated by the commas.
Syntax:
print("{} {} {}".format(variable1, variable2, variable2)Example:
# Python program to print multiple variables # using format() method name = "Mike" age = 21 country = "USA" print("{} {} {}".format(name, age, country)) print("Name: {}, Age: {}, Country: {}".format(name, age, country))Output:
Mike 21 USA Name: Mike, Age: 21, Country: USAMethod 3:
By using the new-style string formatting with numbers (format() method), we can also print the multiple variables. This is similar to method 2 but here we can use the numbers inside the curly braces ({0}), it will help for reordering the values.
Note: Number 0 represents the first variable in format() method, 1 represents the second, and so on.
Syntax:
print("{0} {1} {2}".format(variable1, variable2, variable2)Example:
# Python program to print multiple variables # using format() method with numbers name = "Mike" age = 21 country = "USA" print("{0} {1} {2}".format(name, age, country)) print("Name: {0}, Age: {1}, Country: {2}".format(name, age, country)) print("Country: {2}, Name: {0}, Age: {1}".format(name, age, country)) # printing all values 2-2 times print("{0} {0} {1} {1} {2} {2}".format(name, age, country))Output:
Mike 21 USA Name: Mike, Age: 21, Country: USA Country: USA, Name: Mike, Age: 21 Mike Mike 21 21 USA USAMethod 4:
By using the new-style string formatting with explicit names (format() method), we can also print the multiple variables. This is similar to method 3 but here we can use the explicit names inside the curly braces ({n}), it will help for remembering the order and variable names.
Syntax:
print("{v1} {v2} {v3}".format(v1=variable1, v2=variable2, v3=variable2)Example:
# Python program to print multiple variables # using format() method with explicit names name = "Mike" age = 21 country = "USA" print("{n} {a} {c}".format(n=name, a=age, c=country)) print("Name: {n}, Age: {a}, Country: {c}".format(n=name, a=age, c=country)) print("Country: {c}, Name: {n}, Age: {a}".format(n=name, a=age, c=country)) # printing all values 2-2 times print("{n} {n} {a} {a} {c} {c}".format(n=name, a=age, c=country))Output:
Mike 21 USA Name: Mike, Age: 21, Country: USA Country: USA, Name: Mike, Age: 21 Mike Mike 21 21 USA USAMethod 5:
We can print multiple variables by concatenating them as a string.
Syntax:
print(str(variable1) + str(variable2) + str(variable3))Note:
- If we want to display any message or separator, we can also concatenate them with the variables.
- If a variable is a string, then there is no need to use str().
Example:
# Python program to print multiple variables # using string concatenation name = "Mike" age = 21 country = "USA" print("Without separator...") print(name + str(age) + country) print("Separating by commas...") print(name + "," + str(age) + "," + country) print("Printing with messages...") print("Name: " + name + " Age: " + str(age) + " Country: " + country)Output:
Without separator... Mike21USA Separating by commas... Mike,21,USA Printing with messages... Name: Mike Age: 21 Country: USAPython Tutorial