What is the meaning of the % notation?

Hi Guys

I am doing a python control exercise. I looked at the answers and for a print string it has come up with this

print(“cheapest option available is $.2f% with %s shipping.” %(cost,method))

i know that .2f means float to 2dp and the %s refers to a string.

What do the % mean in this case and what is its purpose?

I have a link to my exercise

https://www.codecademy.com/paths/data-science/tracks/dspath-functions-and-logic/modules/dspath-control-flow/projects/sals-shipping

Thanks
Ben

1 Like

The % in this instance is an argument placeholder, the type of which to expect is denoted by f and s. The one outside the string is what ties the argument list to the placeholders, in the order in which they appear in the list.

1 Like

I just came here to ask the same question. I am new to Python, and am working my way through the Python 3 course.

I managed to code the exercise for Sal’s Shipping successfully, but when I watched the video at the end, this notation caught me off-guard, as it was never described in the previous lessons (unless I missed it). This was a bit frustrating to come across this method in the solution when it was never discussed in the course material. I am hoping it is described later on, and the video erroneously used something prematurely.

One possibility could be that the course author and the video host are not on the same page (or in the same building) or that they even know each other. It may be that the video host completed the project with no direction from the author, using one’s own knowledge, and preferences. That might explain why the old string formatting method was used on the video, and the newer format method used in the course. Mind, this is still conjecture.

That’s just it - there hasn’t been any discussion of string formatting at all thus far. The only mention of the % sign was for the modulo operation.

Some of the pro learning paths do tend to jump ahead a bit. If you’re finding yourself running into this sort of thing from time to time, put the path on hold for a few days and complete the Python 2 and Python 3 tracks. This will give you more background in the rudiments, or at least some exposure to concepts that may be missing in the pro line.