What is % and how is it used?


#1

I am working on: https://www.codecademy.com/courses/learn-python-3/projects/sals-shipping

I was doing this project and everything was all good until I came across the tutorial video on the bottom for it.
The guy uses this:

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

(cost and method are already defined)

I don’t remember learning about this in any of the lessons. What does % do? Does it basically act as a variable? If so, why is there a .2 for the dollar one?

I thought it was to show the remainder of division…

Just what is that in general?


#2

It is a symbol, and nothing more. It indicates a formatting instruction to be encapsulated with the placeholder, starting with the expected type.


#3

% is no longer the preferred way of doing string interpolation, it should instead say:

"The.cheapest option available is {:.2f} with {} shipping".format(cost, method)