Couldn't understand of "%" use here

image
I was followind the course and i correcly created the program as i told but when i watched project walkthrough i noticed a “%” use. Actually, i was coding same until the “%” part of the function. Can anyone explain me of utilisation of “%” ?

It’s a rather outdated method of formatting strings, you are passing cost and method so that they’re converted to a string format and added into that string where the %f and %s placeholders are.

Generally the .format() method is preferred (I think it was backported to at least Python 2.6/2.7, not sure about before then) and in Python3 f-strings are also an option (but .format retains valid use too). Rather than provide an example myself I quite like this page for some information and a few examples on formatting strings in Python:


There are numerous other guides on string formatting in Python if you wanted to hunt one down yourself instead.
3 Likes