pyformat.info icon indicating copy to clipboard operation
pyformat.info copied to clipboard

PEP 498 -- Literal String Interpolation

Open danielniccoli opened this issue 7 years ago • 8 comments

Would it be beneficial to add f-strings to this cheat sheet? I hope this is not vastly out the website's focus.

>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."

danielniccoli avatar Dec 13 '17 12:12 danielniccoli

I agree, I came here to open the same issue.

I can give it a try and add some examples to thank the thousand times I've opened the website when I didn't remember how to format().

fepegar avatar Dec 31 '17 00:12 fepegar

@fepegar That would be awesome! I do not use f-strings yet so I can't do it myself, but I am sure it will be very useful to have such a recap on this awesome new feature!

lrq3000 avatar Feb 17 '18 15:02 lrq3000

I also came here to propose this, but considering that a current task of this page is

All examples on this page work out of the box with with Python 2.7, 3.2, 3.3, 3.4, and 3.5 without requiring any additional libraries.

f-strings don't fit in here, since they are not backward-compatible.


However, I think f-strings are great and Python 3 is too, so IMO it's worth to include them on this really very helpful page : )

martin-martin avatar Apr 16 '18 14:04 martin-martin

f-strings don't fit in here, since they are not backward-compatible.

OTOH there are suggestions to add notes about format_map in other issues and @ulope seems broadly favorable, so the backwards compatibility bit could probably be dropped. Not to mention people seem to be using pyformat as a one-stop shop for formatting, (I certainly do).

Incidentally, either this or #24 should be closed.

xmo-odoo avatar Feb 27 '19 09:02 xmo-odoo

Incidentally, either this or #24 should be closed.

#24 is limited to contributors.

I want to help. What can I do?

Simply adding or translating examples would be easy. However, it appears to me there is more to be done. For starters, how to present the coexistence of .format() and f"{}"? And how to approach the exclusivity of f"{}" to Python 3.6+?

MatthijsBlom avatar May 02 '19 16:05 MatthijsBlom

@martin-martin I don't share your opinion and I don't see a reason why f-strings don't belong here as they are used to format strings.

The examples on this page work out of the box without requiring any additional libraries. If not tagged with a version, the examples work with Python 2.7, 3.2, 3.3, 3.4, and 3.5.

danielniccoli avatar Jun 18 '19 05:06 danielniccoli

Hey everyone,

thanks for being so interested in the continued evolving of the page :)

Just to weigh in here why it's taking so long to get a new updated version (with f-strings) out: The 'problems' are basically exactly what @MatthijsBlom mentioned.

Almost all current examples use literals (e.g. "{:02d}".format(1)). For f-strings that would become "{1:02d}". That's not very easy to parse visually and also not at all a typical use case. With more complex examples it would get even worse. Since I want this page to remain very easy to understand I've opted to rather keep it as is until I find a good solution. Of course one 'simple' solution could be to just assign a dummy variable for every f-string example, but that feels pretty clunky and would take up additional screen space.

Also there is currently no good place to explain the syntactic differences between .format and f-strings.

For all those reasons I've come to the conclusion that the current structure needs an overhaul in order to support those additions.

I really hope I'll find some time over the summer to work on this.

ulope avatar Jun 18 '19 07:06 ulope

Shameless plug - a few days ago, I launched a new site instead, fstring.help (repo). It focuses solely on f-strings (thus avoiding the issues regarding the current structure). Much of the content is based on pyformat.info (with proper credit, I hope - thanks @ulope, @zerok and others!). I indeed ended up adding some variables to make the f-strings more readable. IMHO that works out fine.

The-Compiler avatar Apr 20 '22 07:04 The-Compiler