Live data from Hacker News

Python string literals are kinda funny

sebsite.pw

31–40 of 50 posts

Re: Python string literals are kinda funny

#31

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…

[deleted]

Re: Python string literals are kinda funny

#32
post #7

Earlier 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.

[deleted]

Re: Python string literals are kinda funny

#33
post #5

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.

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…

[deleted]

Re: Python string literals are kinda funny

#34
post #15
post #11

Been 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.

It’s unclear what the big advantage is over `print("Age: "+age)`. It’s even more characters, in that specific example.

Re: Python string literals are kinda funny

#35
post #18

Whenever I read people's takes about Python on HN, I always feel like I'm using an entirely different language.

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

#36
post #14
post #11

Been 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.

Personally I do find the first version clearer, because it is based on general language rules.

Re: Python string literals are kinda funny

#37
post #30
post #8

Earlier 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…

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

#38
post #34
post #15

Earlier 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.

If age is a number, that won’t work.

Re: Python string literals are kinda funny

#39
post #30

Earlier 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.

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.

[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

#40
post #34

Earlier 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.

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.
Post reply on HN