3 ms·
age = 32 print(f"Age: {age}") Thus concludes the lecture on f-strings.
by andai 3mo ago
age = 32
print(f"Age: {age}")
Thus concludes the lecture on f-strings.
- kzrdude 3mo agoI find it funny and satisfying that Python has converged to it's own "printf", by which I mean print(f"").
- layer8 3mo agoIt’s unclear what the big advantage is over `print("Age: "+age)`. It’s even more characters, in that specific example.
- kstrauser 3mo agoIf age is a number, that won’t work.
- mixmastamyk 3mo agoToy examples aren’t the use case, rather multiple variables or expressions, perhaps with formatting (padding) as well. f-string is shorter, less err prone, and faster for medium complexity or above.
- deleted 3mo ago[deleted]
- andai 3mo ago>>> 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...)