3 ms·
I learned this nice syntax from a tweet yesterday: >>> import datetime >>> now = datetime.datetime.now() >>> f"Today is {now:%m/%d/%Y}." 'Today
by japhyr 6y ago
I learned this nice syntax from a tweet yesterday:
>>> import datetime
>>> now = datetime.datetime.now()
>>> f"Today is {now:%m/%d/%Y}."
'Today is 01/20/2021.'
No need for now.strftime() in simple string output!
https://twitter.com/mariatta/status/1351359518316216321 https://twitter.com/mariatta/status/1351359518316216321
- CGamesPlay 6y agoThank you! This was literally what I opened the article for (I'd seen the f syntax previously but hadn't paid much attention) and it's conspicuously missing!
- wrigby 6y agoI love this - didn't realize it was a thing at all! If anyone is curious, this works for any type in Python that implements __format__ ({now:%m/%d/%Y} is equivalent to {now.__format__('%m/%d/%Y')}), and you can (ostensibly) make the format string mean whatever you want.
- gitanovic 6y agoWhile I really do like this, I cannot avoid remarking that python starts to look more and more like perl