john-science / python_for_scientists

Python Open Courseware for Scientists and Engineers
GNU General Public License v3.0
68 stars 40 forks source link

Mention f-strings in Advanced Strings lecture. #79

Closed mrzachsnyder closed 1 year ago

mrzachsnyder commented 1 year ago

There are many wonderful writeups, e.g. Google's first search result: https://realpython.com/python-f-strings/

tl;dr embedding variables in your string is easier and more readable with f-strings.

Example using string.format() "{} minutes | ({}) Move {} assembly from {} to {}".format(travelTime, self.name, self.assyType, self.locationName, nextLocationName)

Same example with f-string f"{travelTime} minutes | ({self.name}) Move {self.assyType} assembly from {self.locationName} to {nextLocationName}"

john-science commented 1 year ago

This is a great idea!

john-science commented 1 year ago

Tell me if you like this (albeit short) introduction to f-strings!

https://github.com/john-science/python_for_scientists/commit/62a8b6e501665dd9671e390a154bf1d8352fc67e

I am happy to add to it, and build on it.

mrzachsnyder commented 1 year ago

Looks good to me, thank you for adding f-strings!