age = 32
print(f"Age: {age}")
Thus concludes the lecture on f-strings.
print(f"Age: {age}")
Thus concludes the lecture on f-strings.
f'{a} = {functionThatReturnsAButHasSideEffects()}'
f-string is shorter, less err prone, and faster for medium complexity or above.
>>> age = 32
>>> print("Age: " + age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print("Age: " + str(age))
Age: 32
As a side note, I always found it amusing that Python is dynamic but doesn't let you do this, while C# has static typing but lets you do string + number. (I looked into it a while back, I think it's because both are Object, so it does operator overloading on Object+Object and then checks the types...)