Python 3: How to understand this string

Asked

Viewed 168 times

3

I have a question, I am a beginner and I am reading the book "Introduction to Programming with Python" 3° Edition. And I came across this code below.

Program 4.3 - Income Tax Calculation

salario = float(input('Digite o salário para cálculo do imposto.: '))
base = salario
imposto = 0

if base > 3000:
    imposto = imposto + ((base - 3000) * 0.35)
    base = 3000
if base > 1000:
    imposto = imposto + ((base - 1000) * 0.20)

print(f'Salário: R${salario:6.2f} Imposto a pagar: R${imposto:6.2f}')

It is working but in this 'print' I did not understand "6.2f" being if I remove it will work the same way as in the example below.

Program 4.3 - Income Tax Calculation

salario = float(input('Digite o salário para cálculo do imposto.: '))
base = salario
imposto = 0

if base > 3000:
    imposto = imposto + ((base - 3000) * 0.35)
    base = 3000
if base > 1000:
    imposto = imposto + ((base - 1000) * 0.20)

print(f'Salário: R${salario} Imposto a pagar: R${imposto}')
  • Have a look at "Formatted string literals" in the manual: https://docs.python.org/3/reference/lexical_analysis.html#f-strings

2 answers

7

Good afternoon Francisco, all good?

Francis, about the ':6.2f' is the following:

This corresponds to an output formatting in Python called Formatted Literal Strings or f-strings, as they are also known.

ABOUT THE ':6' : The number 6 after ':' helps you set the minimum size of this field by imposing a minimum number of characters, which basically serves to adjust the width of the field. If the field does not have at least 6 characters, Python adjusts it for you, and the width of the field will be at least 6 characters. If You test in your code and change this number by 10 you will see that the space between the 'R$' and the result gets bigger. Do tests, change 6 by 100 and you will see that the size only increases. The whole number after the ':', which in your script is 6, serves for this.

ABOUT THE '.2f' :
The . 2f is used to control the decimals, that is, after the comma, which in the case of Python is a '.' and not a comma, you will have two numbers. Just for example, if the result is R$ 2500.00, the . 2f worked to make sure that after the point you had two decimal places, as in the 00 example. Run tests, replace 2 by 10 and you’ll see that the number of decimals will increase.

About this type of formatting the Python documentation has some very interesting things, I’ll leave a link that can help.

https://docs.python.org/pt-br/3/tutorial/inputoutput.html#Tut-f-strings

I hope I helped. Good luck Francisco.

4


Documentation: https://docs.python.org/3/reference/lexical_analysis.html#f-strings

These are string formatting - the link above explains the options used with the 'format' method, which work the same way for f-strings. After the expression, inside a pair of keys in an "f-string", you can use the "!" , ":" or "=" (Python 3.8) to specify how the result of the expression will be shown. In the case of ":6.2f" it means: "format the number as a decimal with 6 digits, being 2 after the decimal point". By default, the output is aligned to the right, filling the remaining digits with spaces".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.