5 different ways to print multiple values in Python

How to print multiple values in Python:

By using print, we can print single or multiple values in Python. There are different ways we can use to print more than one value in one line. In this post, I will show you four different ways to print multiple values in Python with examples.

Method 1: Pass multiple parameters to print:

We can pass more than one variable as parameter to print. It will print them in one line. For example:

print('Hello', 'World', 123, '!!')

It will print:

Hello World 123 !!

Method 2: %s and tuple:

We can pass %s and a tuple to print multiple parameters.

print('%s World %s %s' % ('Hello', 123, '!!'))

It will print:

Hello World 123 !!

Method 3: %s and dictionary:

Another way is to pass a dictionary:

print('%(3)s World %(2)s %(1)s' % {'3': 'Hello', '2': 123, '1': '!!'})

The output is same.

Method 4: format():

string formatting is another way to do this:

print('{} World {} {}'.format('Hello', 123, '!!'))

Output:

Hello World 123 !!

We can also pass the index values:

print('{0} {0} World {1} {2}'.format('Hello', 123, '!!'))

It will print:

Hello Hello World 123 !!

Or, we can assign names:

print('{h} {h} World {o} {l}'.format(h='Hello', o=123, l='!!'))

It will print the same output.

Method 5: f-string:

Starting from python 3, we can also use f-string or formatted string literals to print multiple variables in a single line:

word_1 = 'Hello'
word_2 = 'World'
word_3 = '!!'

print(f'Printing this line: {word_1} {word_2} {word_3}')

It will print:

Printing this line: Hello World !!

You might also like: