4 ms·
In python 3.6 and up, that becomes def example_cheater(color, flavor, age): return f'I am {age} years old and I like to wear {color} hats and eat {fl
by avbor 8y ago
In python 3.6 and up, that becomes
def example_cheater(color, flavor, age):
return f'I am {age} years old and I like to wear {color} hats and eat {flavor} icecream'
Even nicer!
- luhn 8y agoFor reference, this feature is Formatted String Literals ("f-strings"). https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals https://docs.python.org/3/reference/lexical_analysis.html#fo...
- shaklee3 8y agoOf course I didn't read your comment until several Google searches later.
- nazgul17 8y agoDidn't know this. Thanks for sharing!
- dec0dedab0de 8y agoOMG! that is so exciting, Thank you for telling me! I have only been using 3.x as my primary language for a few months now, I can't wait to try this out. I feel like a huge dork right now, because that made my day.
- sys_64738 8y agoDoes this trip up pylint?
- nercht12 8y agoTaken straight out of Ruby, I see. Very convenient feature (and one of my favorites about Ruby), though it does contradict the do-it-one-way mentality.
- bhrgunatha 8y agoAccording to Rosetta Code Algol 68 had string interpolation. [1] It'd be interesting to see if there are any older languages that supported it too. https://rosettacode.org/wiki/String_interpolation_(included)#ALGOL_68 https://rosettacode.org/wiki/String_interpolation_(included)...
- setr 8y ago>though it does contradict the do-it-one-way mentality. not really: use f-strings when you're passing in variables as-is (or nearly no work), and format() when you need to do work on them before stringifying them; f-strings are naturally (and obviously) more difficult to read when the variables are big, as it obscures the actual text they're being fit into, and where. The only natural area for preference to apply is whether to do the work before the format() call, name the variables, and change it to an f-string.. or stick with a multi-line format() I'm not sure theres any real situation where the choice isn't obvious. Maybe if you're doing something like f"list1: {sorted(a)}\nlist2{sorted(b)}", where the work is rather small, but even then f"list1:{}\nlist2:{}".format(sorted(a), sorted(b)) is just as nice, or rather unsatisfying, as the f-string.
- masklinn 8y agof-strings are also unusable for i18n.
- nercht12 8y ago> not really: Yes it does. You could construct the string using a for-loop. Thank you for proving my point and then down-voting me.
- setr 8y ago...if you’re going down that route, you could claim that any feature not necessary for turing completeness conflicts with the one way to do it strategy, since you could alternatively build up whatever feature from scratch, thus offering additional solutions But practically, there remains (close to) one obvious way to do things, with the choice boiling down to whether or not to name your outputs before string construction (and the choice of f-string vs format naturally falling out). But thats always been a choice. On a side note: I don’t use comment voting systems, for up or down votes, on pretty much any site including HN. Even if I did, I don’t see why you’d care
- chestervonwinch 8y agoNeat: >>> pi = 3.14159; two = 2 >>> f"{two:02d} pi is {2*pi:.2f}" '02 pi is 6.28'