How do I insert multiple variables with string formatting?

Question

How do I insert multiple variables with string formatting?

Answer

So long as your number of %s placeholders matches the number of variables you are providing after the %, it will insert them in the order in which you provide them. Take a look over the code below for a better understanding:

var1 = “awesome”
var2 = “ever”
print “Codecademy has the most %s coding lessons %s!” % (var1, var2)
# displays: “Codecademy has the most awesome coding lessons ever!”

If you don’t have the same number of placeholders as variables provided to it, you will get an error message like these:
TypeError: not all arguments converted during string formatting
TypeError: not enough arguments for format string
The first means you have more variables than placeholders, and the second means you have more placeholders than variables provided. This is a very common issue, so be on the lookout!

10 Likes

2 posts were split to a new topic: Why Are There No Backslashes Here?

How would this be written for Python 3

The modulo format is still valid in Python 3. You may be interested to learn how str.format() works.

"The {} in {} falls mainly in the {}".format('rain', 'Spain', 'plains')

This page compares both methods…

https://pyformat.info/

8 Likes

Something to notice about the str.format() method …

def spam():
    print('Sam-I-am')
    return 'am'

print('S{0}-{1}-{0}'.format(spam(), 'I'))

What will the output be? Execute the code to find out.

How may times did the spam() function get called?

4 Likes

Hi mtf, I have a question with this lesson.

We will use always the letters “s” and “d” after the %?

http://www.informit.com/articles/article.aspx?p=28790&seqNum=2

2 Likes

Wow! Amazing, thank you so much.

1 Like

You are welcome. If you liked that, then the PyFormat link above is right in tow. One expects an eventual update to include f-strings but for now it is a nice comparison of % Tuple and str.format().

Also, don’t forget to use “print()” because “print” is a function in Python 3. In Python 2 it is just “print” and then whatever you want to print after it.

The example does not work on repl.it, nor does the course material on this. Could it be the different Python versions?

Could be. The course is very likely python2, doesn’t repl offer python2 anymore? Anyway, if you use python3 you would have to make some changes. Like @violetl.8197767400 mentioned, print statement (python2) vs print() function call (python3) is a big difference. I am sure you can find all the differences online

1 Like
day = 6
print "03 - %02d - 2019" % (day)
# 03 - 06 - 2019

The lesson tells me that ‘d’ is a signed integer. But what is a signed integer and what are it’s uses?

https://www.tutorialspoint.com/python/python_numbers.htm

int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.

I recently worked in PHP, we have unsigned integers in PHP, meaning the value can’t be negative.

1 Like
str1 = "nice"
print("This is %" %(str1.upper()))

Could you tell me why the code isn’t working?
(It’s showing a value error and an incomplete format)

your string formatting operation is incomplete:

https://docs.python.org/2/library/stdtypes.html#string-formatting

1 Like

3?
i’m a beginner lol

Hi @robertcastellar64096,

This is the output:

Sam-I-am
Sam-I-am

Based on that result, how many times did the spam() function get called here?

EDIT (October 2, 2020):

Here’s a clue:

calls = 0
def count_calls():
    global calls
    calls += 1
    return calls

print('{0} {0} {0}'.format(count_calls()))

Output:

1 1 1

How many times was the count_calls function called?