0
distancia = float(input("Digite a distância em metros: "))
km = float(distancia/1000)
centimetros = distancia*100
print("A medida de {}m corresponde a {:.0f}km e {:.0f}cm".format(distancia, km, centimetros))

I was writing this code and the AI from github suggested me the final line, but I don't know why it used the ":.0" before the "f" to use the interpolation in this line, can someone explain me this?

Thanks.

I was just trying to practice a little bit, do some exercises.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • `:.0f` gives the string representation of a float rounded to the nearest integer. `'{:.0f}'.format(1.88)` gives `'2'` whereas `'{:.1f}'.format(1.88)` gives `'1.9'`. – Steven Rumbalski Jul 05 '23 at 13:31
  • It's common to prefer f-strings over .format(): `f"A medida de {distancia}m corresponde a {km:.0f}km e {centimetros:.0f}cm"` – Steven Rumbalski Jul 05 '23 at 13:34

0 Answers0