As per the python documentation:
2.4.3. Formatted string literals
New in version 3.6.
A formatted string literal or f-string is a string literal that is
prefixed with 'f'
or 'F'
. These strings may contain replacement
fields, which are expressions delimited by curly braces {}
. While
other string literals always have a constant value, formatted strings
are really expressions evaluated at run time.
There are multiple examples in the documentation, so I'll post a few of them and explain:
name = "Fred"
f"He said his name is{name!r}."
# "He said his name is 'Fred'.
Here the !
introduces a conversion field. !r
calls repr()
The result is then formatted using the format()
protocol. The format specifier is passed to the __format__()
method of the expression or conversion result. An empty string is passed when the format specifier is omitted. The formatted result is then included in the final value of the whole string.
Since it's formatted using the format()
protocol, the following are other use-cases:
width = 10
precision = 4
value = decimal.Decimal("12.34567")
f"result: {value:{width}.{precision}}"
# result: 12.35
Even datetime objects:
today = datetime(year=2017, month=1, day=27)
f"{today:%B %d, %Y}"
# January 27, 2017
Taking the information above, let's apply it to your code:
f'([{string.punctuation}“”¨«»®´·º½¾¿¡§£₤‘’])'
The line above is inserting string.punctuation
into the string at that location.
According to the docs, string.punctuation
is:
String of ASCII characters which are considered punctuation characters in the C
locale.
If you really want to dig deeper into this: What's the C
locale?
The C standard defines the locale as a program-wide property that may be relatively expensive to change. On top of that, some implementation are broken in such a way that frequent locale changes may cause core dumps. This makes the locale somewhat painful to use correctly.
Initially, when a program is started, the locale is the C
locale, no matter what the user’s preferred locale is.