Doubt about the replace method()
The replace () method is a method of the str class, correct?
Follows a code that' runs ' perfectly where the object is an integer (or at least in my interpretation). Or in the case of interpolation does it change?
Follows:
def moeda(valor, moeda = 'R$'):
return f'{moeda}{valor:.2f}'.replace('.', ',')
velocidade = float(input('Qual é a velocidade do carro? (KM) '))
if velocidade > 80:
print('VELOCIDADE PERMITIDA ULTRAPASSADA!')
print('\033[1;31mMULTADO!\033[m')
multa = (velocidade - 80) * 6
multa = moeda(multa)
print(f'Você foi multado em {multa}')
else:
pass
PS. The currency () function transforms an integer into currency format by replacing the integer points with commas.
0
1 answers
As you said yourself, replace
is a method of strings and values of type int
or float
Not have it.
What happens in your code is that you pass the value into a new string using the string formatting f-string. Therefore, you are using the replace
method of a str
created in formatting and not a numeric value. See the example below:
valor = 75.99
string_valor = f"R$ {valor}"
print(type(string_valor), "-", string_valor) # <class 'str'> - R$ 75.99
string_valor = string_valor.replace(".", ",")
print(string_valor)
1
Author: JeanExtreme002, 2020-02-05 02:48:09