Print multiple variables in 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?

Printing single variable

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: USA

Printing 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: Passing multiple variables as arguments separating them by commas

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: USA

Method 2: Using format() method with curly braces ({})

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: USA

Method 3: Using format() method with numbers in curly braces ({0})

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 USA

Method 4: Using format() method with explicit name in curly braces ({v})

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 USA

Method 5: Using string concatenation

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: USA

Python Tutorial

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.