I would like to know what the function of: {} in the Python language is and more specifically within the PRINT function as in this code?

3

I would like to know the function of: {} in the Python language and more specifically within the PRINT function as in this code?

Salario = int(input('Salario? '))
imposto = 27.
while imposto >0.:
    imposto = input('Imposto ou (0) para sair: ')
    if not imposto:
            imposto = 27.
    else:
            imposto=float(imposto)
    print("Valor Real: {0}".format(Salario-(Salario*(imposto*0.01))))
    
asked by anonymous 28.12.2016 / 17:09

2 answers

5

{0} means the replacement position of this placeholder by a value or variable. The idea is that it works as a mask this type of construction avoids excessive concatenations.

format() resembles the printf() function of php, where the placeholders are defined (in this case {0} , {n} ) and what will be the respective values the order of them must be respected otherwise an unexpected result / output will happen.

Example taken from documentation :

>>> print('{0} and {1}'.format('spam', 'eggs'))
spam and eggs
>>> print('{1} and {0}'.format('spam', 'eggs'))
eggs and spam

Recommended reading:

Signature of format ()

    
28.12.2016 / 17:17
1

{0} will be replaced by the return of the format function. More details here link

    
28.12.2016 / 17:17