Skip to content

Tweaks to string-formatting concept#4099

Open
BNAndras wants to merge 5 commits intoexercism:mainfrom
BNAndras:update-string-formatting
Open

Tweaks to string-formatting concept#4099
BNAndras wants to merge 5 commits intoexercism:mainfrom
BNAndras:update-string-formatting

Conversation

@BNAndras
Copy link
Member

No description provided.

@BethanyG
Copy link
Member

You should add yourself to the contributors list here and the links doc might need an update or once-over. 🙂 More comments as I finish up my coffee.

Copy link
Member

@BethanyG BethanyG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

>>> verb = 'meet'

# This example includes a function, str, a nested f-string, an arithmetic expression,
# This example includes a function, an arithmetic expression,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

- `!<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].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@BNAndras
Copy link
Member Author

BNAndras commented Mar 2, 2026

I'll take a closer look after work about adding an example for escapes not being applied and the formatting / alignment examples as well.

Copy link
Member

@BethanyG BethanyG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# 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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice addition. 😄

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants