What is %d and %f in the code given below | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is %d and %f in the code given below

Example:- n1, n2 = 1, 2 print('%d + %d = %d' % (n1, n2, n1 + n2)) n3 = 4 print('square root of %f is %f' % (n3, n3**0.5)) Here what is %d and %f

21st Aug 2020, 4:23 PM
Amit Kumar
Amit Kumar - avatar
2 Answers
+ 6
I just wanted to add a comment, that there is also an other way of formatting output by using f-strings. See a comparison of both ways. f-strings does not need to have extra placeholders. Variables and expressions can be written in curly braces. Also adjustment and column width can be defined. n3 = 4 print('square root of %f is %f' % (n3, n3**0.5)) print(f'square root of {n3:d} is {n3**0.5:.2f}') # :d -> format integer, :.2f -> format float with 2 decimal places # result : square root of 4.000000 is 2.000000 square root of 4 is 2.00
21st Aug 2020, 5:24 PM
Lothar
Lothar - avatar
+ 4
%d is specifier for Integer type. %f for float type data.. Those are replaced by respective arguments from %({0},{1},{2},...) in the string farmat of output by print statement.. Just check code by running playground to see output and observe it to understand it better.. These are syntax gathered from C. So it same as C specifiers...
21st Aug 2020, 4:32 PM
Jayakrishna 🇮🇳