#DynamicFormatting

Dr. Juande Santander-Velajuandesant@astrodon.social
2024-11-26

Today I learned combined with Public Service Announcement:

In Python, you can use format strings with variables not just as the values that need to be represented, but also as parameters for the values!

I.e., if you want to print a given number in scientific notation, you can do:

```python
> value = 314159265
> print(f”{value:6.2e}“)
3.14e+06
```

But if you want to personalize the printing in significant digits and floating point digits you can do:

```python
> digits = 12 # total number of digits
> fl_digits = 4 # for the floating point part
> value = 314159265
> print(f"{value:{digits}.{fl_digits}e}")
3.1416e+06
```
#Python #precision #NumericPrecision #NumberFormatting #DynamicFormatting #DynamicNumericPrecision #TIL #PSA #TodayILearned #PublicServiceAnnouncement

Client Info

Server: https://mastodon.social
Version: 2025.04
Repository: https://github.com/cyevgeniy/lmst