Hello I was practicing using loops and I saw that while using concatenation I have to convert int's to strings, but then I saw that if I use a f-string I dont need convert it to an int.
Here is the code:
# some code up there
average_cost = [total_cost / len(actual_insurance_costs)]
# using a f-string
print(f"Average Insurance Cost: {average_cost} dollars.")
# using concatenation
print("Average Insurance Cost: " + str(average_cost) + " dollars.")
So does the f-string convert an int into a string?
Yes, it does, by way of calling
format(...)on it, which in turn calls__format__()on the object.That extends to everything else too, including objects with a custom
__format__(). For instance,prints out
(The default implementation of
__format__()just calls__str__().)