Python string literals are kinda funny(sebsite.pw) |
Python string literals are kinda funny(sebsite.pw) |
I feel like 90% of new Python features in the last 10 years just increased language complexity without any benefit.
Basic usage is a no-brainer: write your string, put variables or short expressions in curly braces, add `printf`-style format specifiers after the colon. This is also great because it's a natural extension of `printf`-style format strings.
Of course you can write complicated and confusing f-strings. But then you can write complicated and confusing... anything, really. Many programming languages have extremely weird quirks and cases where basic syntax can be transformed into an unreadable monstrosity, like C syntax for pointers to functions and arrays.
Sure, this increases the language's complexity, but you don't have to use all of it to reap the benefits.
Because these are used for locale-specific configuration data. `"This is here: {x+y} blah"` on the other hand isn’t a string (mere data), it’s a program, because you can have arbitrary expressions inside the braces. You don’t want to repeat the `x+y` in each localization file.
I have nothing against ergonomic program constructs for composing strings, but please let’s not confuse such program constructs with mere string literals.
"The thing is {foo}, and also {foo} again".format(foo=x+y)
It also supports positional with empty {}. And like f-strings, you can put formatting information after a colon.%-based printf-style did also have named variables like this but it seemed less known.
1. %-formatting [1]
2. str.format [2]
3. string.Template [3]
4. f-string [4]
5. t-string [5]
What happened to "one-- and preferably only one --obvious way to do it"? [6]
Also, the way string formatting interacts with logging is a total mess. People just pass f-strings to logging, which seems to be an intuitive way to do it. Except it limits your options if you want to collect structured logs and it doesn't allow you to use late evaluation based on log level.
[1] https://docs.python.org/3/library/string.html#format-example...
[2] https://docs.python.org/3/library/stdtypes.html#str.format
[3] https://docs.python.org/3/library/string.html#string.Templat...
[4] https://docs.python.org/3/reference/lexical_analysis.html#f-...
[5] https://docs.python.org/3/reference/lexical_analysis.html#t-...
There arguably still is—just use f-strings for everything, unless you need to support ancient Python, in which case use %-formatting.
t-strings are a special case, but in theory most functions should only accept regular strings or templates, so there should only be one choice there too.
> Also, the way string formatting interacts with logging is a total mess [...] it doesn't allow you to use late evaluation based on log level.
This all seems to be a side-effect of the fact that string formatting produces static strings, so I don't think that there's much that can be done here (but maybe t-strings can be creatively used here somehow).
The language exposes so much of its internals that even if the design were consistent, the ecosystem of (buggy) libs and tools makes it inconsistent.
>>> f'{'}'}' '}'
Huh? I remember learning the rule that you can't nest quotes inside fstrings if they are the same kind (unlike in bash) - e.g
f"{mydict["foo"]}"
would be a syntax error, but f"{mydict['foo']}"
or f'{mydict["foo"]}'
would be valid.The reason being the same like for the rstring weirdness: The lexer comes first and identifies the string literal, then for fstrings, the python parser is invoked again for each {...} expression to parse it.
This is unlike other nested expressions, which are already split up by the lexer and then parsed in one go.
Did that change at some point?
There was a PEP about it:
https://stackoverflow.com/questions/78388333/nested-quotes-i...
https://docs.python.org/3.12/whatsnew/3.12.html#pep-701-synt...
Instead of reading like 10 PEPs for f strings, I just use the + operator on strings and backslash escaping, big whoop.
print(f"Age: {age}")
Thus concludes the lecture on f-strings.
Been using python for longer than 10 years and immediately started using f-strings when I could. It takes almost no time to understand the basics.
VI (not even VIM) on minimal Debian installs does not have syntax highlighting.