logoalt Hacker News

Python string literals are kinda funny

56 pointsby jandeboevrielast Thursday at 4:44 AM41 commentsview on HN

Comments

PyWoodytoday at 8:37 PM

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

show 1 reply
dwdztoday at 7:37 PM

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.

show 5 replies
phyzometoday at 8:19 PM

Goofy edge cases aside, I think f-strings are great.

vova_hn2today at 9:15 PM

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.

ethintoday at 9:06 PM

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.

cocodilltoday at 8:36 PM

do not get the funny part of the pythons string.

xg15last Thursday at 6:25 AM

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.

Did that change at some point?

show 1 reply
madpropstoday at 8:39 PM

Great new way to write blog posts!

smitty1etoday at 7:24 PM

Whenever I'm building a JSON document, I revert to the old %s syntax just because it's more tidy than an f-string.

show 1 reply
TZubiritoday at 8:10 PM

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.

show 4 replies