Live data from Hacker News

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

realpython.com

1–10 of 147 posts

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

#3
post #2

I can't say I like the f" syntax much. I think JS's backtick is better. Other than that, I agree, much better than % and less verbose than .format. It's just the f" or f' that doesn't sit right with me

It's consistent with other strings in Python like r-strings and the older u-strings.

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

#4
post #2

I can't say I like the f" syntax much. I think JS's backtick is better. Other than that, I agree, much better than % and less verbose than .format. It's just the f" or f' that doesn't sit right with me

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.

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

#6
Not mentioned in the article, but with Python 3.8, f-strings have gained the ability to print debug information using `=` specifier.

    name = 'world'
    print(f'hello {name.upper()=}')
outputs:

    hello name.upper()='WORLD'

It's small, but very useful in debugging an issue or logging something to the console.

https://docs.python.org/3/whatsnew/3.8.html#f-strings-suppor...

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

#7
post #4
post #2

I can't say I like the f" syntax much. I think JS's backtick is better. Other than that, I agree, much better than % and less verbose than .format. It's just the f" or f' that doesn't sit right with me

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.

You can use any of

    f"{foo['field']}"
    f'{foo["field"]}'
    f"""{foo["field"]}"""
(I see from your carefully worded first sentence that you already knew this but still find it annoying, but I'll leave this here for any that aren't aware.)

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

#8
post #4
post #2

I can't say I like the f" syntax much. I think JS's backtick is better. Other than that, I agree, much better than % and less verbose than .format. It's just the f" or f' that doesn't sit right with me

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.

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

#10
post #4
post #2

I can't say I like the f" syntax much. I think JS's backtick is better. Other than that, I agree, much better than % and less verbose than .format. It's just the f" or f' that doesn't sit right with me

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.

I'm surprised it works like that in Python. C# has similar interpolated strings but there it's legal to embed quotation marks within the curly brackets. Though I guess it's less necessary in Python since you can just use the other type of quote inside.
Post reply on HN