Earlier 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…
Python string literals are kinda funny
31–40 of 50 posts
Re: Python string literals are kinda funny
#32Earlier 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.
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
#33I'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.
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…
Re: Python string literals are kinda funny
#34Been 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.
age = 32 print(f"Age: {age}") Thus concludes the lecture on f-strings.
Re: Python string literals are kinda funny
#35Whenever I read people's takes about Python on HN, I always feel like I'm using an entirely different language.
Re: Python string literals are kinda funny
#36Been 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.
Right, because x + " " + y is more clear than of simply f"{x} {y}". 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.
Re: Python string literals are kinda funny
#37Earlier 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…
> 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…
Re: Python string literals are kinda funny
#38Re: Python string literals are kinda funny
#39Earlier quoted context omitted.
> 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…
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.
[0] though GNU libc does let you extend it: https://sourceware.org/glibc/manual/latest/html_mono/libc.ht...
Re: Python string literals are kinda funny
#40Earlier quoted context omitted.
It’s unclear what the big advantage is over `print("Age: "+age)`. It’s even more characters, in that specific example.
If age is a number, that won’t work.