Hacker News new | past | comments | ask | show | jobs | submit login
Python f-strings can do more than you thought. f'{val=}', f'{dt:%Y-%m-%d}' (youtube.com)
3 points by abbassi 36 days ago | hide | past | favorite | 2 comments



f-strings in Python are not just a way to easily substitute variables inside strings, they come with a wide array of conversion, formatting, and debugging options which we give examples of in this video.

Note: I'm trying a more informal style in this video so make sure to let me know if you like it better this way, as opposed to my usual cut-after-every-sentence editing.


I haven't looked for this in the doc, but the f-string formatting is recursive:

>>> import math

>>> num_value = math.pi # to get long fractional part

>>> precision = 2

>>> print(f'{num_value:.{precision}f}')

3.14

>>> precision = 5

>>> print(f'{num_value:.{precision}f}')

3.14159




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: