9 ms·
It’s unclear what the big advantage is over `print("Age: "+age)`. It’s even more characters, in that specific example.
by layer8 2mo ago
It’s unclear what the big advantage is over `print("Age: "+age)`. It’s even more characters, in that specific example.
- kstrauser 2mo agoIf age is a number, that won’t work.
- mixmastamyk 2mo 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 2mo ago[deleted]
- andai 2mo 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...)