I'm not a fan of f-strings. I feel like 90% of new Python features in the last 10 years just increased language complexity without any benefit.
Python string literals are kinda funny
41–50 of 67 posts
Re: Python string literals are kinda funny
#42Earlier quoted context omitted.
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?
It's been a long time since I've had to SSH into a box to fix an issue in production. But it is comforting to know that I _could_. VI (not even VIM) on minimal Debian installs does not have syntax highlighting.
Most IDEs support editing files on a remote machine through SFTP.
Re: Python string literals are kinda funny
#43Earlier quoted context omitted.
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.
Are f-strings not easy for beginners? What would you prefer instead?
Re: Python string literals are kinda funny
#44Earlier quoted context omitted.
If age is a number, that won’t work.
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
#45Earlier quoted context omitted.
age = 32 print(f"Age: {age}") Thus concludes the lecture on f-strings.
It’s unclear what the big advantage is over `print("Age: "+age)`. It’s even more characters, in that specific example.
f-string is shorter, less err prone, and faster for medium complexity or above.
Re: Python string literals are kinda funny
#46Earlier quoted context omitted.
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 i…
> 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…
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
#47Re: Python string literals are kinda funny
#48Earlier quoted context omitted.
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.
In Python, that less hazardous string concatenation operator is an f-string.
Re: Python string literals are kinda funny
#49Earlier quoted context omitted.
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 se…
Those aren't the only two options. Like GP I don't like f-strings, but there was something introduced before that: the format function. "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.
Re: Python string literals are kinda funny
#50Been using python for almost 10 years. I never use any of the funky strings. Instead of reading like 10 PEPs for f strings, I just use the + operator on strings and backslash escaping, big whoop.
Instead of 10 PEPs, you could read one helpful cheatsheet: https://www.pythonmorsels.com/string-formatting/#cheat-sheet... .
- 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.