Re:Python string literals are kinda funny
by xg15 on Thursday August 06, 2026 @02:25AM
(#49193185)
here's a valid f-string:
>>> 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.
Re:Python string literals are kinda funny
by globular-toast on Saturday August 08, 2026 @05:07PM
(#49225923)
Wow, I didn't know this either. I find it a bit crazy. It must make no sense without syntax highlighting. But I suppose who writes code without highlighting any more?
Re:Python string literals are kinda funny
by vova_hn2 on Sunday August 09, 2026 @02:29AM
(#49228927)
No, I was talking about SFTP, not scp. OpenSSH server has SFTP enabled by default. You can mount it using sshfs or browse it using any SFTP client (or any program with embedded SFTP client, which is most IDEs).
So, if "digging around" means looking at folders' structure, you can do it through SFTP without copying everything (unlike scp or rsync).
And if digging around means running commands, nothing prevents you from having a session for running commands open in parallel.
Re:Python string literals are kinda funny
by dotancohen on Sunday August 09, 2026 @09:13AM
(#49231095)
I block [s]ftp at the firewall and disable it. I also keep ssh open only after port knocking. After reading logs and such, it's easier to open files with copy and paste via screen or tmux. Or even `Atl-.` depending on what I'm doing, framework, etc.
Re:Python string literals are kinda funny
by vova_hn2 on Sunday August 09, 2026 @12:04PM
(#49232653)
How exactly do you block sftp at the firewall if it uses the same port as ssh? From the firewall's perspective it is just some encrypted traffic inside ssh tunnel. I think that you might be mistaking sftp with ftps (ftp through ssl/tls tunnel), these are different protocols. I suggest you try connecting to any box you have access to using Sftp protocol and ssh credentials and see the result.
Re:Python string literals are kinda funny
by dotancohen on Sunday August 09, 2026 @12:36PM
(#49232985)
I completely forgot that sftp is on 22. Like I said, I block the port until it is properly knocked. In any case, I usually don't need the convenience of an IDE. It's much easier for me to just do everything over ssh. My IDEs (Jrtbrains, VS Code) use VIM keybindings too. And I could just as easily apt get vim on the server.
In any case, it's literally been years since I've had to.
Re:Python string literals are kinda funny
by ForceBru on Saturday August 08, 2026 @04:03PM
(#49225368)
I'm a HUGE fan of f-strings and I think they should be added to more or less every language in existence. (This is just to show how much I like f-strings, not to be taken literally) `printf`-style format strings seem outdated: why use format strings when I can put my variables IN THE STRING? I want the result of `x+y` to be put {HERE} in this string. Well, just write `"This is here: {x+y} blah"` — it makes perfect sense and immediately lets me see what the resulting strings generally look like.
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.
Re:Python string literals are kinda funny
by layer8 on Saturday August 08, 2026 @05:45PM
(#49226191)
> why use format strings
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.
Re:Python string literals are kinda funny
by zdragnar on Saturday August 08, 2026 @06:01PM
(#49226305)
If your string needs to be presented in multiple locales, you probably want to go a step further and use something like the ICU syntax and a proper parser and formatter rather than rely on manually formatting things yourself. Otherwise, that dynamic data is going to give you headaches when you have to deal with plurals and genders and such.
Re:Python string literals are kinda funny
by layer8 on Saturday August 08, 2026 @06:06PM
(#49226332)
I’m not sure what you mean by manual formatting. You do need a place in your program where you fill in the parameters into the respective localized string template. I agree that the printf format syntax is somewhat limited for localization [0]. But whatever localization string format you use, you don’t want arbitrary expressions embeddable within it.
Re:Python string literals are kinda funny
by ajrouvoet on Saturday August 08, 2026 @04:08PM
(#49225398)
It is baffling to me that Python seems to insist on reinventing language features and coming up with the most incomprehensible of designs. The whole dataclass and serialisation ecosystem comes to mind, as well as the evolution of typing.
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.
Re:Python string literals are kinda funny
by LPisGood on Saturday August 08, 2026 @04:09PM
(#49225409)
PKL files and the entire multiprocessing paradigm is one of the worst experiences I’ve ever had with a programming language feature. Surely there has to be a better way.
Re:Python string literals are kinda funny
by analog31 on Saturday August 08, 2026 @03:52PM
(#49225255)
Indeed, and I get that one can ignore those features (I do), but finding them in existing code or having the AI coding agent use them diminishes the "easy for beginners" aspect.
Re:Python string literals are kinda funny
by analog31 on Saturday August 08, 2026 @06:25PM
(#49226471)
I'd prefer one way of doing things. I do understand that the improvements to string literals are improvements, but it means there are multiple things to learn.
Re:Python string literals are kinda funny
by ForceBru on Saturday August 08, 2026 @05:35PM
(#49226116)
How is, say, `age = 5; print(f"Age: {age} years old")` not easy for beginners? IMO it's as easy as it gets: you want the value of `age` printed {HERE}, so you just put it where you want it, surrounded by curly brackets.
Re:Python string literals are kinda funny
by vova_hn2 on Saturday August 08, 2026 @05:36PM
(#49226122)
I like f-strings but I don't like that there are at least five ways to format stings.
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.
Re:Python string literals are kinda funny
by gucci-on-fleek on Saturday August 08, 2026 @05:41PM
(#49226165)
> What happened to "one-- and preferably only one --obvious way to do it"?
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).
Re:Python string literals are kinda funny
by vova_hn2 on Saturday August 08, 2026 @06:32PM
(#49226529)
> maybe t-strings can be creatively used here somehow
Maybe they could, but I would be the first to oppose introducing creative ways to do logging or string formatting.
Such basic things should be done in the most standard way possible to reduce cognitive effort required to read and understand the code.
But the standard way is kinda ugly. Most people would expect f-strings to be used for "normal" string formatting and %-style to be used for logging, because it is the default and most codebases do it this way. Therefore, you basically forced to have (at least) two different formatting syntaxes in your codebase.
I say "at least", because if the program serves html pages, you most likely also have some other template engine like jinja...
Re:Python string literals are kinda funny
by TZubiri on Saturday August 08, 2026 @07:28PM
(#49226869)
Especially if you come from other languages. a + " " + b will be clearer to devs that aren't senior python developers. the f string thing is just a marginal improvement at the cost of alienating other devs. But it's great for gatekeeping and job security.
Re:Python string literals are kinda funny
by shooly on Saturday August 08, 2026 @08:20PM
(#49227159)
> come from other languages
According to Wikipedia[1] the history of string formatting goes back to 1950s. Also, basically every major programming language these days implements it in some form.
Re:Python string literals are kinda funny
by layer8 on Saturday August 08, 2026 @06:10PM
(#49226359)
Admittedly I’m not so familiar with Python; it does work in Java. In that case, introducing a less hazardous string concatenation operator would seem more universally useful.
Re:Python string literals are kinda funny
by andai on Saturday August 08, 2026 @09:09PM
(#49227420)
>>> age = 32
>>> print("Age: " + age)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> print("Age: " + str(age))
Age: 32
As a side note, I always found it amusing that Python is dynamic but doesn't let you do this, while C# has static typing but lets you do string + number. (I looked into it a while back, I think it's because both are Object, so it does operator overloading on Object+Object and then checks the types...)
Re:Python string literals are kinda funny
by Walf on Saturday August 08, 2026 @08:07PM
(#49227084)
That you know how to use the language without such oddities presenting any difficulty does not make them less strange. That r-string parsing solution is fine for the interpreter, but we are not interpreters, so it's not a logical outcome for Python authors,
The most puzzling thing is few languages use the absolute simplest solution to escaping quotes, which happens to be especially useful for non-expanded literals, and that's good ol' quote-doubling. Difficult for Python to introduce now since it'd be a bc-break for implied concatenation, but if space were required between them, we could have had
x = r'ex\x20cape!\'
y = 'diff''rent'
z = 'diff' 'erent'
respectively containing
ex\x20cape!\
diff'rent
different
TOML has a similar issue: it is impossible to store its delimiter for non-expanded multi-line strings inside a multi-line non-expanded string. There's no method to escape it. Whilst this is rarely an issue in practice, it's odd that there's unnecessary difficulty in writing about TOML inside a TOML document. Again, it could have used the simpler quote-doubling method for all strings (even easier because no concatenation), with similar rules for non-expanded and multi-line variants. Then there would be no limitation on what can be stored in any literal type. Instead it has a peculiarity that's illogical and offers no benefit to us authors.
Re:Python string literals are kinda funny
by kstrauser on Saturday August 08, 2026 @06:00PM
(#49226298)
I feel ya. I don’t write a lot of it anymore, but have written probably hundreds of thousands of lines over the years. It has a few rough edges, but over all it’s solid and I’ve used it to make lots things I’m proud of.
Re:Python string literals are kinda funny
by ethin on Saturday August 08, 2026 @05:06PM
(#49225908)
Not really about the content of the blog post but am I the only one bothered by the complete lack of capitalization? Granted it may be because of my screen reader but my TTS engine of choice doesn't pause on un-capitalized words/sentences/phrases/etc. which follow a full stop, so this post unless I read it line by line (minus the code) just blends into complete noise.
Re:Python string literals are kinda funny
by vova_hn2 on Saturday August 08, 2026 @05:15PM
(#49225971)
I think this is pretty intuitive (I was able to answer the question correctly before opening the spoiler), but I really like raw string designs in Rust and C++11 that allow you to stop worrying about escaping completely.
Re:Python string literals are kinda funny
by DonHopkins on Sunday August 09, 2026 @03:06PM
(#49234574)
In his 2017 PyCon Israel keynote, "The Fun of Reinvention", Dave Beazley made a wonderfully mischievous case for taking advantage of new Python features instead of making every new project carry the accumulated baggage of old Python versions.
Talking about Python 3.6, he said it was "probably one of the most major Python releases that has ever been made." His demonstration intentionally used new features that made the code incompatible with older interpreters. This was a prototype, so why not use the interesting new tools?
He was especially enthusiastic about f-strings: "F-strings are just awesome."
I was originally skeptical about them, but he convinced me to reconsider. One implementation detail that later helped win me over was that CPython 3.6 added dedicated FORMAT_VALUE and BUILD_STRING opcodes for f-strings. That does not mean an f-string is faster than simply doing a + b when both values are already strings -- simple concatenation usually wins that particular race -- but f-strings are generally cleaner, and often faster than older formatting machinery such as str.format().
I agree with his argument. There are vanishingly few situations in which a new project genuinely must support ancient Python releases. If your employer refuses to let you use a reasonably current version without a concrete technical reason, that is a warning sign. Life is too short to program indefinitely for obsolete interpreters.
>>> 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
would be a syntax error, but or 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...
VI (not even VIM) on minimal Debian installs does not have syntax highlighting.
Most IDEs support editing files on a remote machine through SFTP.
So, if "digging around" means looking at folders' structure, you can do it through SFTP without copying everything (unlike scp or rsync).
And if digging around means running commands, nothing prevents you from having a session for running commands open in parallel.
In any case, it's literally been years since I've had to.
json_doc = '''{}''' % some_dictionary
and then populate json_doc via %(some_dictionary_key)s values inside of the brackets.
I find it more readable.
Furthermore, json_doc can now live elsewhere, if sizeable, and get imported.
I guess that template strings may be the newer way to do this, but this is very backward compatible.
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.
%-based printf-style did also have named variables like this but it seemed less known.
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.
[0] though GNU libc does let you extend it: https://sourceware.org/glibc/manual/latest/html_mono/libc.ht...
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.
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-...
[6] https://peps.python.org/pep-0020/
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).
Maybe they could, but I would be the first to oppose introducing creative ways to do logging or string formatting.
Such basic things should be done in the most standard way possible to reduce cognitive effort required to read and understand the code.
But the standard way is kinda ugly. Most people would expect f-strings to be used for "normal" string formatting and %-style to be used for logging, because it is the default and most codebases do it this way. Therefore, you basically forced to have (at least) two different formatting syntaxes in your codebase.
I say "at least", because if the program serves html pages, you most likely also have some other template engine like jinja...
You can write things like that without SQL injection:
This is a very pleasant feature, very useful[1] https://www.psycopg.org/psycopg3/docs/basic/tstrings.html
Instead of reading like 10 PEPs for f strings, I just use the + operator on strings and backslash escaping, big whoop.
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.
According to Wikipedia[1] the history of string formatting goes back to 1950s. Also, basically every major programming language these days implements it in some form.
[1] https://en.wikipedia.org/wiki/Printf
> senior python developers > great for gatekeeping and job security
Ah, okay, it's just a troll.
"%T", T t
> Ah, okay, it's just a troll.If that helps you sleep at night
print(f"Age: {age}")
Thus concludes the lecture on f-strings.
f-string is shorter, less err prone, and faster for medium complexity or above.
f'{a} = {functionThatReturnsAButHasSideEffects()}'
- the trailing slash detail detailed in the OP
- raw strings at all
- more complex stuff like raw format strings
Also, my mind is a temple, I don't learn python from cheatsheets from random secondary sources.
The most puzzling thing is few languages use the absolute simplest solution to escaping quotes, which happens to be especially useful for non-expanded literals, and that's good ol' quote-doubling. Difficult for Python to introduce now since it'd be a bc-break for implied concatenation, but if space were required between them, we could have had
x = r'ex\x20cape!\'
y = 'diff''rent'
z = 'diff' 'erent'
respectively containing
ex\x20cape!\
diff'rent
different
TOML has a similar issue: it is impossible to store its delimiter for non-expanded multi-line strings inside a multi-line non-expanded string. There's no method to escape it. Whilst this is rarely an issue in practice, it's odd that there's unnecessary difficulty in writing about TOML inside a TOML document. Again, it could have used the simpler quote-doubling method for all strings (even easier because no concatenation), with similar rules for non-expanded and multi-line variants. Then there would be no limitation on what can be stored in any literal type. Instead it has a peculiarity that's illogical and offers no benefit to us authors.
Talking about Python 3.6, he said it was "probably one of the most major Python releases that has ever been made." His demonstration intentionally used new features that made the code incompatible with older interpreters. This was a prototype, so why not use the interesting new tools?
He was especially enthusiastic about f-strings: "F-strings are just awesome."
I was originally skeptical about them, but he convinced me to reconsider. One implementation detail that later helped win me over was that CPython 3.6 added dedicated FORMAT_VALUE and BUILD_STRING opcodes for f-strings. That does not mean an f-string is faster than simply doing a + b when both values are already strings -- simple concatenation usually wins that particular race -- but f-strings are generally cleaner, and often faster than older formatting machinery such as str.format().
I agree with his argument. There are vanishingly few situations in which a new project genuinely must support ancient Python releases. If your employer refuses to let you use a reasonably current version without a concrete technical reason, that is a warning sign. Life is too short to program indefinitely for obsolete interpreters.
The keynote:
https://www.youtube.com/watch?v=js_0wjzuMfc
Beazley's other talks:
https://www.dabeaz.com/talks.html