Print multiple arguments in Python

1,083,320

Solution 1

There are many ways to do this. To fix your current code using %-formatting, you need to pass in a tuple:

  1. Pass it as a tuple:

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

A tuple with a single element looks like ('this',).

Here are some other common ways of doing it:

  1. Pass it as a dictionary:

    print("Total score for %(n)s is %(s)s" % {'n': name, 's': score})
    

There's also new-style string formatting, which might be a little easier to read:

  1. Use new-style string formatting:

    print("Total score for {} is {}".format(name, score))
    
  2. Use new-style string formatting with numbers (useful for reordering or printing the same one multiple times):

    print("Total score for {0} is {1}".format(name, score))
    
  3. Use new-style string formatting with explicit names:

    print("Total score for {n} is {s}".format(n=name, s=score))
    
  4. Concatenate strings:

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

The clearest two, in my opinion:

  1. Just pass the values as parameters:

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

    If you don't want spaces to be inserted automatically by print in the above example, change the sep parameter:

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

    If you're using Python 2, 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
    
  2. Use the new f-string formatting in Python 3.6:

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

Solution 2

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 {} and {} is {}".format(a,b,c)) 

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

EDIT :

#New f-string formatting from Python 3.6:
print(f'Sum of {a} and {b} is {c}')

Solution 3

Use: .format():

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

Or:

// Recommended, more readable code

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

Or:

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

Or:

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

Or: f-string formatting from Python 3.6:

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

Can use repr and automatically the '' is added:

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

# or for advanced: 
print(f'Total score for {name!r} is {score!r}') 

Solution 4

In Python 3.6, f-string is much cleaner.

In earlier version:

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

In Python 3.6:

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

will do.

It is more efficient and elegant.

Solution 5

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))
Share:
1,083,320
user1985351
Author by

user1985351

Updated on July 31, 2022

Comments

  • user1985351
    user1985351 almost 2 years

    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.

  • Snakes and Coffee
    Snakes and Coffee about 11 years
    of course, there's always the age-old disapproved method: print("Total score for "+str(name)"+ is "+str(score))
  • Blender
    Blender about 11 years
    @SnakesandCoffee: I'd just do print("Total score for", name, "is", score)
  • pepr
    pepr about 11 years
    My +1. These days I prefer the .format() as more readable than the older % (tuple) -- even though I have seen tests that show the % interpolation is faster. The print('xxx', a, 'yyy', b) is also fine for simple cases. I recommend also to learn .format_map() with dictionary as the argument, and with 'ssss {key1} xxx {key2}' -- nice for generating texts from templates. There is also the older string_template % dictionary. But the templates do not look that clean: 'ssss %(key1)s xxx %(key2)s'.
  • ShadowRanger
    ShadowRanger over 7 years
    FYI, as of Python 3.6, we get f-strings, so you can now also do print(f"Total score for {name} is {score}") with no explicit function calls (as long as name and score are in scope obviously).
  • theQuestionMan
    theQuestionMan over 3 years
    @SnakesandCoffee Why is print("Total score for "+str(name)"+ is "+str(score)) disapproved?
  • theQuestionMan
    theQuestionMan over 3 years
    @SnakesandCoffee Lol...got the answer...some other guy also had the same question...stackoverflow.com/questions/41008941/…
  • Wolf
    Wolf almost 3 years
    The first comment (#Normal string concatenation) is at least misleading.
  • Wolf
    Wolf almost 3 years
    I know this is quite old. But what about the new f"I have been {a} for {b} years"? I'm going by only that one recently...
  • Wolf
    Wolf almost 3 years
    When exactly should .format be used, now that you have f-strings?
  • Wolf
    Wolf almost 3 years
    BTW: when are you using " and when ' quotation?
  • Joe Race
    Joe Race about 2 years
    Is there one that is common for Python 2 and Python 3 ?