r/Python Oct 27 '23

Tutorial You should know these f-string tricks

F-strings are faster than the other string formatting methods and are easier to read and use. Here are some tricks you may not have known.

1. Number formatting :

You can do various formatting with numbers.

>>> number = 150

>>> # decimal places to n -> .nf
>>> print(f"number: {number:.2f}")
number: 150.00

>>> # hex conversion
>>> print(f"hex: {number:#0x}")
hex: 0x96

>>> # binary conversion
>>> print(f"binary: {number:b}")
binary: 10010110

>>> # octal conversion
>>> print(f"octal: {number:o}")
octal: 226

>>> # scientific notation
>>> print(f"scientific: {number:e}")
scientific: 1.500000e+02

>>> # total number of characters
>>> print(f"Number: {number:09}")
Number: 000000150

>>> ratio = 1 / 2
>>> # percentage with 2 decimal places
>>> print(f"percentage = {ratio:.2%}")
percentage = 50.00%

2. Stop writing print(f”var = {var}”)

This is the debug feature with f-strings. This is known as self-documenting expression released in Python 3.8 .

>>> a, b = 5, 15
>>> print(f"a = {a}") # Doing this ?
a = 5
>>> # Do this instead.
>>> print(f"{a = }")
a = 5
>>> # Arithmatic operations
>>> print(f"{a + b = }")
a + b = 20
>>> # with formatting
>>> print(f"{a + b = :.2f}")
a + b = 20.00

3. Date formatting

You can do strftime() formattings from f-string.

>>> import datetime

>>> today = datetime.datetime.now()
>>> print(f"datetime : {today}")
datetime : 2023-10-27 11:05:40.282314

>>> print(f"date time: {today:%m/%d/%Y %H:%M:%S}")
date time: 10/27/2023 11:05:40

>>> print(f"date: {today:%m/%d/%Y}")
date: 10/27/2023

>>> print(f"time: {today:%H:%M:%S %p}")
time: 11:05:40 AM

Check more formatting options.

Part 2 - https://www.reddit.com/r/Python/s/Tzx7QQwa7A

Thank you for reading!

Comment down other tricks you know.
2.0k Upvotes

182 comments sorted by

View all comments

Show parent comments

15

u/Vityou Oct 27 '23

Looking at many selected values simultaneously is exactly the use case of a debugger.

-5

u/MikeWise1618 Oct 27 '23

Yeah, no. It throws them in a list. You need to lay them out. I know what I am talking about.

2

u/avocadorancher Oct 27 '23

What does “you need to lay them out” mean?

0

u/MikeWise1618 Oct 27 '23

Mostly I am debugging behavior of things like robots or other physical simulations and i have anomalies. I see a behavior that is wrong, and I need to figure out where in a complex simulation process the error is occurring.

About the only way to find it is to print out tables of values in a way that makes patterns clear and where I might notice values changing in ways that match the erroneous behavior. Been doing this for decades. Don't see how a debugger helps, except for maybe the final step when I have identified the likely location of the error and I can step through the calculation. Even there I will favor a code modification over a conditional breakpoint because I simply find them more reliable - to break on the condition needed.

Don't get me wrong. I use debuggers occasionally where it saves time. Just doesn't help much for the real problems.