0

Im kind of having trouble understanding the use of this sign %, I thought its only for solving remainders in python but I saw my prof using this to print a statement. Here's an example code that I dont get; enter image description here

in the image, i dont get the highlighted in red text which is the use of %d and **%.3f** Is the letter d fixed? or i can use any other alphabet characters for that? What and when can I use it? What is it called in python so that maybe i can searched it on youtube

ayokona
  • 17
  • 4
  • Python uses C-style string formatting to create new, formatted strings. The "%" operator is used to format a set of variables enclosed in a "tuple" (a fixed size list), together with a format string, which contains normal text together with "argument specifiers", special symbols like "%s" and "%d". link: https://www.learnpython.org/en/String_Formatting – LearningNoob Oct 21 '20 at 13:36
  • Does this answer your question? [What is the meaning of Percent sign in Python?](https://stackoverflow.com/questions/30764897/what-is-the-meaning-of-percent-sign-in-python) ; or https://stackoverflow.com/questions/4288973/whats-the-difference-between-s-and-d-in-python-string-formatting – Tomerikoo Oct 21 '20 at 13:52
  • This is the oldest string formatting option in Python. You would be better off investigating about `f-strings` which are the most up-to-date way of formatting strings – Tomerikoo Oct 21 '20 at 13:54

1 Answers1

0

It is a placeholder, %d for decimal value, %f for float, %s for string etc.. used for injecting in formatted string.

For more usage see: What's the difference between %s and %d in Python string formatting?