Conversation
BethanyG
left a comment
There was a problem hiding this comment.
This largely looks good (nice work!). I might have more adjustments as we get the exercise going, but for now the changes are good and my suggestions are minimal.
Still noodling on if we want the related exercise to cover choosing between the methods, or if we only want to focus on f-strings. My first impulse is to do a mix of str.format() and f-strings, and leave the other stuff out. I worry that although template strings are important, they will confuse students, especially if/when we introduce Python 3.14s T-strings.
concepts/string-formatting/about.md
Outdated
| >>> verb = 'meet' | ||
|
|
||
| # This example includes a function, str, a nested f-string, an arithmetic expression, | ||
| # This example includes a function, an arithmetic expression, |
There was a problem hiding this comment.
Since the new specifications are explicit about both nesting and not needing to switch out the quotes, should we now include examples of nesting and switching quotes so that escapes don't get applied in results?
concepts/string-formatting/about.md
Outdated
| - `!<conversion>` is optional and should be one of this three conversions: `!s` for [`str()`][str-conversion], `!r` for [`repr()`][repr-conversion] or `!a` for [`ascii()`][ascii-conversion]. | ||
| By default, `str()` is used. | ||
| - `:<format_specifier>` is optional and has a lot of options, which we are [listed here][format-specifiers]. | ||
| - `:<format_specifier>` is optional and has a lot of options, which are [listed here][format-specifiers]. |
There was a problem hiding this comment.
We might want to go into some detail here with different number formatting/alignment, since it's pretty common for formatting outputs in a table. Trey Hunner has some good examples.
|
I'll take a closer look after work about adding an example for escapes not being applied and the formatting / alignment examples as well. |
BethanyG
left a comment
There was a problem hiding this comment.
I like this version! Just a few small nits, but nice work overall. 😄
| # Here, we pull a value from the dictionary by using the key | ||
| >>> f'Tenfold the value of "light" is {waves["light"] * 10}.' | ||
| 'Tenfold the value of "light" is 30.' | ||
| # A f-string can be nested inside another f-string |
There was a problem hiding this comment.
| # A f-string can be nested inside another f-string | |
| # An f-string can be nested inside another f-string |
| `f-string` expressions cannot be empty, they cannot contain comments. | ||
| There are two main limitations to be aware of. | ||
| `f-string` expressions can not be empty. | ||
| [Additionally, before Python 3.12, they can not contain comments.][pep-0701] |
There was a problem hiding this comment.
| [Additionally, before Python 3.12, they can not contain comments.][pep-0701] | |
| [Additionally, before Python 3.12, they could not contain comments.][pep-0701] |
| [`string.Template()`][string.Template()] (_not to be confused with Python 3.14 [t-strings]_) is a class from the `string` module (_as opposed to the built-in `str` type_), which is part of the Python standard library, but has to be imported for use. | ||
| Template strings support `$`-based substitution and are much simpler and less capable than the other options mentioned here. | ||
| However, they can be very useful for when complicated internationalization is needed, or outside inputs need to be sanitized. | ||
| `string.Template` is considered safer for untrusted user input because it prevents evaluating arbitrary expressions or accessing object attributes, which mitigates format-string injection attacks. |
| 3. If simplicity, safety, and/or heavy internationalization is what you need, `string.Template()` can be used to mitigate risks when inputs need to be handled and for wrapping translation strings. | ||
| 4. The `%` operator should mostly be used for compatibility with old code. | ||
| `%` formatting` can lead to issues displaying non-ascii and unicode characters and has more errors and less functionality than other methods. | ||
| However, given Python's long history and different considerations, it might not be surprising that there are **three** other common ways to perform string formatting in Python: |
There was a problem hiding this comment.
| However, given Python's long history and different considerations, it might not be surprising that there are **three** other common ways to perform string formatting in Python: | |
| However, given Python's long history and different use-cases, it might not be surprising that there are **three** other common ways to perform string formatting in Python: |
No description provided.