edgi-govdata-archiving / ECHO-Sunrise

An Environmental Enforcement Watch partnership with Sunrise Boston hubs
https://colab.research.google.com/github/edgi-govdata-archiving/ECHO-Sunrise/blob/main/ECHO-Sunrise.ipynb
GNU General Public License v3.0
1 stars 2 forks source link

Use f-strings for nicer formatting #62

Closed Mr0grog closed 3 years ago

Mr0grog commented 3 years ago

I was watching @maalvikabhat's video demo of this notebook today (nice job!) and noticed that some of the text output didn't include commas in the numbers, which would have been nice. It's pretty easy to do when using f-strings, so I went ahead and updated those, and then all the rest of the strings to use them.

Python's f-strings are typically much more readable than string concatenation ("hello " + some_variable + "!"), interpolation ("hello %s!" % (some_variable)), or string.format() ("hello {}!".format(some_variable)) because the variables/expressions are directly inline within the string. They also offer some fancy formatting options that none of the others (except string.format) do. For example, you can add thousands separators to numbers:

>>> x = 6543245
>>> f"There were {x:,} results"
"There were 6,543,245 results"

(More docs on options at: https://docs.python.org/3/library/string.html#formatspec)

It also doesn't hurt that f-strings are faster, CPU-wise, than all other options. ;)

shansen5 commented 3 years ago

Thanks for the tip, Rob! Steve

On Fri, Oct 2, 2020 at 3:37 AM Rob Brackett notifications@github.com wrote:

I was watching @maalvikabhat https://github.com/maalvikabhat's video demo of this notebook today (nice job!) and noticed that some of the text output didn't include commas in the numbers, which would have been nice. It's pretty easy to do when using f-strings, so I went ahead and updated those, and then all the rest of the strings to use them.

Python's f-strings are typically much more readable than string concatenation ("hello " + some_variable + "!"), interpolation ("hello %s!" % (some_variable)), or string.format() ("hello {}!".format(some_variable)) because the variables/expressions are directly inline within the string. They also offer some fancy formatting options that none of the others (except string.format) do. For example, you can add thousands separators to numbers:

x = 6543245>>> f"There were {x:,} results""There were 6,543,245 results"

(More docs on options at: https://docs.python.org/3/library/string.html#formatspec)

It also doesn't hurt that f-strings are faster, CPU-wise, than all other options. ;)

You can view, comment on, or merge this pull request online at:

https://github.com/edgi-govdata-archiving/ECHO-Sunrise/pull/62 Commit Summary

  • Use f-strings for nicer formatting

File Changes

Patch Links:

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/edgi-govdata-archiving/ECHO-Sunrise/pull/62, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAF2XQ6LZNPGP2TYASH2CD3SIWUOXANCNFSM4SBRGITQ .

ericnost commented 3 years ago

Hi @Mr0grog I'm sorry I missed this. Thank you so much!! I have merged the PR.