Home » Python » Print multiple arguments in python

Print multiple arguments in python

Posted by: admin November 1, 2017 Leave a comment

Questions:

This is just a snippet of my code:

print("Total score for %s is %s  ", name, score)

but I want it to print out:
“Total score for (name) is (score)” where name is a variable in a list and score is an integer. This is python 3.3 if that helps at all.

Answers:

Pass it as a tuple:

print("Total score for %s is %s  " % (name, score))

Or use the new-style string formatting:

print("Total score for {} is {}".format(name, score))

Or pass the values as parameters and print will do it:

print("Total score for", name, "is", score)

If you don’t want spaces to be inserted automatically by print, change the sep parameter:

print("Total score for ", name, " is ", score, sep='')

If you’re using Python 2, you won’t be able to use the last two because print isn’t a function in Python 2. You can, however, import this behavior from __future__:

from __future__ import print_function

Questions:
Answers:

There are many ways to print that.

Let’s have a look with another example.

a = 10
b = 20
c = a + b

#Normal string concatenation
print("sum of", a , "and" , b , "is" , c) 

#convert variable into str
print("sum of " + str(a) + " and " + str(b) + " is " + str(c)) 

# if you want to print in tuple way
print("Sum of %s and %s is %s: " %(a,b,c))  

#New style string formatting
print("sum of {0} and {1} is {2}".format(a,b,c)) 

#in case you want to use repr()
print("sum of " + repr(a) + " and " + repr(b) + " is " + repr(c))

Questions:
Answers:

Keeping it simple, I personally like string concatenation:

print("Total score for " + name + " is " + score)

It works with both Python 2.7 an 3.X.

NOTE: If score is an int, then, you should convert it to str:

print("Total score for " + name + " is " + str(score))

Questions:
Answers:

just try:

print ( "Total score for", name,"is", score )

Questions:
Answers:

In py 3.6 f-string is much cleaner

In earlier version:

print("Total score for %s is %s  " % (name, score))

In py 3.6:

print(f'Total score for {name} is {score}')

will do.

More efficient and elegant.

Questions:
Answers:

If score is a number, then

print("Total score for %s is %d" % (name, score))

If score is a string, then

print("Total score for %s is %s" % (name, score))

If score is a number, then it’s %d, if it’s a string, then it’s %s, if score is a float, then it’s %f

Questions:
Answers:

Just follow this

idiot_type= "biggest idiot"
year= 22
print("I have been {} for {} years ".format(idiot_type,years))

OR

idiot_type= "Biggest idiot"
year= 22
print("I have been %s for %s years "% (idiot_type,year))

And forget all others else brain wont be able to map all the formats.

Questions:
Answers:
print("Total score for %s is %s  " % (name, score))

%s can be replace by %d or %f

Questions:
Answers:

This is what I do:

print("Total score for " + name + " is " + score)

Remember to put a space after for and before and after is.
Happy coding.