I would like to know how I write> print ("size = %4.2 F cm" % size) and what does the %4.2 F mean

I'm doing a college assignment and would like to know what the %4.2f means in >>> print("Tamanho = %4.2f cm" % Tamanho) and how is a value (8.47826) represented following such a parameter after the program runs

Author: user179733, 2020-03-04

2 answers

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 value 8.4726 would be:

Size = 8.47 cm

 1
Author: Loip, 2020-03-04 19:26:27

This is called formatting, which means that you are telling python that the output formatting of the variable Tamanho that will be displayed in the function print() will have at most 4 houses before the comma and 2 houses after the comma. % indicates the start of formation and f the type of variable you are formatting, i.e. float.

But this is a rather old formatting, today the most used is .format() Take a look at this documentation here:

Https://pyformat.info

 1
Author: Luan, 2020-03-04 21:37:13