News for hackers. Stuff that matters.
Re:Python f-strings are the best syntax sugar i never knew a... by tedmiston on Tuesday January 23, 2018 @07:24PM (#16219236)
You can do cool nested calls with them too.

    >>> class A:
    ...     def __init__(self):
    ...         self.foo = 5
    ...     def bar(self):
    ...         return 'cake'
    ...
    >>> a = A()
    >>> f'{a.foo}'
    '5'
    >>> f'{a.bar()}'
    'cake'

    >>> x = {'b': 1, 'c': 2}
    >>> f"{x['b']}"
    '1'
Re:Python f-strings are the best syntax sugar i never knew a... by tudelo on Wednesday January 24, 2018 @09:53AM (#16223096)
Interesting. Thanks for the post. I always just concatenated them out of laziness, and this looks even lazier.
Re:Python f-strings are the best syntax sugar i never knew a... by odonnellryan on Wednesday January 24, 2018 @12:23PM (#16224413)
It's super nice when you're working on reporting software: software that needs to quickly shoot out a string like this from time-to-time!

It's much nicer to write this:

f"{account_name} {pretty_date(start_date)} - {pretty_date(end_date)} account attribution"

and MUCH easier to maintain than the alternative...!

Re:Python f-strings are the best syntax sugar i never knew a... by pyedpiper on Wednesday January 24, 2018 @01:22PM (#16224960)
saw this after posting, so epicly useful. Alreadying thinking of some nice concise, explanatory custom Exception descriptions..
Re:Python f-strings are the best syntax sugar i never knew a... by in9 on Wednesday January 24, 2018 @08:20AM (#16222401)
it is very very true. F-strings are awesome. But there is one use case that I prefer .format(), and is when formatting a string using a dictionary. Being able to is awesome:

  some_long_string_template.format(**some_dictionary_with_many_keys)
For me that is such a nice and practical use case. But yes for not many local variables, f-string is the way to go.
Re:Python f-strings are the best syntax sugar i never knew a... by EnderMB on Thursday January 25, 2018 @12:58PM (#16232906)
I've only recently started learning Python, and the f-strings remind me a bit of the syntactic sugar provided by C# for string.Format() (var str = $"Hello, my name is {name}."; // for those who care)

I get the same feeling as well, and it's something I noticed a lot when it was a new feature to C#. Suddenly, everyone abandoned string.Format() in favour of $"", even when the code would lose a lot of readability.

Syntactic sugar is good and all, but I find it better to be more verbose for the sake of clarity.

Re:Python f-strings are the best syntax sugar i never knew a... by kristoff_it on Wednesday January 24, 2018 @09:28AM (#16222900)
Templated strings are great for when, as the name suggests, you want to create a string with some placeholders to be expanded at a later time.

When you just want to interpolate a string on the fly, Fstrings are absolutely the right thing to do most of the time.

Re:Python f-strings are the best syntax sugar i never knew a... by pyedpiper on Wednesday January 24, 2018 @01:21PM (#16224952)
true but it seems that the performance gap is pretty huge and the new f-strings actually come in on top for string substitution:

https://cito.github.io/blog/f-strings/

Re:Python f-strings are the best syntax sugar i never knew a... by jxub on Sunday January 28, 2018 @05:51AM (#16250758)
You could also do: "text {}".format(foo) Works in Python 3.5 and earlier ones too.
Re:Python f-strings are the best syntax sugar i never knew a... by dozzie on Tuesday January 23, 2018 @04:02PM (#16217654)
Ah yes, the thing we had in shell, Perl, and Ruby since forever, and that was never a very good idea.
Re:Python f-strings are the best syntax sugar i never knew a... by odonnellryan on Wednesday January 24, 2018 @12:25PM (#16224421)
Why wasn't it a good idea in these languages?
Re:Python f-strings are the best syntax sugar i never knew a... by dozzie on Wednesday January 24, 2018 @05:56PM (#16227132)
Because it's often hard to edit (which includes quoting or decoding where computation or variable starts) and because it mixes formatting and presenting data with computation.
Re:Python f-strings are the best syntax sugar i never knew a... by mailslot on Tuesday January 23, 2018 @03:30PM (#16217351)
Thanks! I didn't know I had literal string interpolation available until today!