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!
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.
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