2
I’m doing a college assignment and I’d like to know what the %4.2f
in >>> print("Tamanho = %4.2f cm" % Tamanho)
and how a value (8.47826) is represented by following this parameter after the program runs
2
I’m doing a college assignment and I’d like to know what the %4.2f
in >>> print("Tamanho = %4.2f cm" % Tamanho)
and how a value (8.47826) is represented by following this parameter after the program runs
1
Assuming you have the following code:
Tamanho = 8.4726
print("Tamanho = %4.2f cm" % Tamanho)
Then the return of the print using the variable Size of value 8.4726 would be:
Size = 8.47 cm
already got! thanks so much for the help!
1
This is called formatting, this means that you are telling python that the variable output formatting Tamanho
that will be displayed in the function print()
have a maximum of 4 boxes before the comma and 2 boxes after the comma. %
indicates the start of training and f
the type of variable you are formatting, ie, float
.
However this is a very old format, today the most used is .format()
take a look at this documentation here:
Browser other questions tagged python python-3.x python-requests
You are not signed in. Login or sign up in order to post.
%4.2f
= F is the typefloat
,4
means at least 4 characters; the2
means two decimal places after the comma.– Luiz Augusto
>>> print("Size = %4.2f cm" % Size) The task is to represent the number 8.47826 within this programming line. However, when I try Size = 8.47 cm appears that it is wrong. I don’t know what else to do.
– user179733
I get it, you want it rounded down: read this question: https://answall.com/questions/44715/como-rounder-um-float-em-python?noredirect=1&lq=1
– Luiz Augusto
I got it, but thank you so much for your help!
– user179733