Live data from Hacker News

Python string literals are kinda funny

sebsite.pw

41–50 of 67 posts

Re: Python string literals are kinda funny

#42

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

How often do you have SSH access to a box but don't have SFTP access?

Most IDEs support editing files on a remote machine through SFTP.

Re: Python string literals are kinda funny

#43
post #12
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.

Are f-strings not easy for beginners? What would you prefer instead?

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

#44
post #40

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

In Python, that less hazardous string concatenation operator is an f-string.

Re: Python string literals are kinda funny

#45
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.

Toy examples aren’t the use case, rather multiple variables or expressions, perhaps with formatting (padding) as well.

f-string is shorter, less err prone, and faster for medium complexity or above.

Re: Python string literals are kinda funny

#46

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…

> 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

#48
post #40

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

I’m assuming you can only use it with string literals, hence it is less universally useful.

Re: Python string literals are kinda funny

#49
post #25
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…

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.

This is still significantly more clunky than f-strings, especially when you're writing them a lot for debugging purposes

Re: Python string literals are kinda funny

#50
post #16
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.

Instead of 10 PEPs, you could read one helpful cheatsheet: https://www.pythonmorsels.com/string-formatting/#cheat-sheet... .

Doesn't cover:

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

Post reply on HN