Live data from Hacker News

Python 3's F-Strings: An Improved String Formatting Syntax

realpython.com

21–30 of 147 posts

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#21
post #8
post #4

Earlier quoted context omitted.

Yup, especially as you often need to access dict's with foo["field"] and you get a syntax error if the f-strings is also f"". JavaScript backtick syntax doesn't lead to this extra syntactic friction.

This is easily avoidable in python by using single quotes for the outer string and double quotes for the inner string, or vice versa. Or, use triple quotes for the outer string.

Yeah, if you get to the level of nesting where having available quotes is an issue, you really need to break it up into multiple expressions.

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#22
A couple of years ago I wrote some Python code which took advantage of this syntax. Then it turned it had to run in an environment without it.

I wrote a simple Awk script to convert Python with f-string syntax to syntax without it.

(The program had a function to do a more or less proper lexical analysis job; not some flimsy regex substitution hack.)

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#23

F-strings have been around for years and this article is at least a few years old. I'm curious why this is coming up now.

I wonder if the submitter was inspired by this tweet that went viral (by python standards) yesterday: https://twitter.com/mariatta/status/1351359518316216321

Capital f-strings should have not been allowed in my opinion.

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#24
post #12

> However, once you start using several parameters and longer strings, your code will quickly become much less easily readable [...] `"Hello, %s %s. You are %s. You are a %s ...` Minor correction: % formatting allows specifying names for dictionary keys too: In [1]: "%(a)s and %(b)s" % {"a":"A", "b":"B"} Out[1]: 'A and B' In fact I had never switched to using str.format as it just never added enough benefit for me ov…

f"{name.lower()} is funny."

The article demonstrates both methods and functions. I just pasted the function example, but without the showing the function definition itself as it's fairly obvious :-)

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#26
When it comes to string formatting I have recently found my absolute favourite utility of all time: Scheme's SRFI-166. It is a combinator based string formatting utility that is user extensible (with verbosity worthy of being in every scheme!): https://srfi.schemers.org/srfi-166/srfi-166.html

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#27

The thing which annoys me most about f strings is I can't build them at runtime, which means I can't just learn/teach f strings. While I know why building f strings is forbidden (they can run arbitrary code), I'm happy to take the risk.

Doesn't calling .format on a string do the exact same thing as an f-string?

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#28

The thing which annoys me most about f strings is I can't build them at runtime, which means I can't just learn/teach f strings. While I know why building f strings is forbidden (they can run arbitrary code), I'm happy to take the risk.

I mean it's Python, so you can do anything:

    >>> fstring="Hello, {\"world\"}"
    >>> eval(f"print(f'{fstring}')")
    Hello, world

Re: Python 3's F-Strings: An Improved String Formatting Syntax

#30

A couple of years ago I wrote some Python code which took advantage of this syntax. Then it turned it had to run in an environment without it. I wrote a simple Awk script to convert Python with f-string syntax to syntax without it. (The program had a function to do a more or less proper lexical analysis job; not some flimsy regex substitution hack.)

Should you ever find yourself in a situation like that, https://github.com/python-rope/rope might help.
Post reply on HN