F-STRINGS in Python do more than you think #shorts
Channel: Lucas Viana
Date: 10/02/2024

Python f-strings have a hidden superpower. The formatting  mini-language is buried in the depths of the documentation, but it offers a lot of flexibility with incredibly few characters. You can format strings, integers and floats using that specification.

For strings you have the option to reserve a width, select a padding character and align left, right or center.

For integers you can convert from decimal to binary, hexadecimal, octal, and there are even some convenience facilities to show the correct prefix (e.g. for binary, prepend the `0b` prefix), and choose a separator to facilitate reading when the output is too long.

For floats , well, floats are complex and so are the formatting options. You can have scientific notation, fixed point, and lots more.

It doesn't stop there. Python uses the `__format__` method to perform the mini-language operation on f-strings, so you can create your own mini-language for your own objects.

Aaaand it doesn't stop there. Ok, I will stop writing now. Just watch the full video to get a better picture:

https://www.youtube.com/watch?v=44DMWikS0Ds&t=789s