Live data from Hacker News

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

realpython.com

41–50 of 147 posts

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

#41
post #29

This is super minor but I really wish f-string formatting were the default behavior for `print` or that there were a `printf` command just for it. Typing that extra f character before the quotes is just annoying enough

Making f-strings the default was discussed at the Python Language Summit last year:

https://pyfound.blogspot.com/2020/04/all-strings-become-f-st...

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

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

Way better -> https://pypi.org/project/q/

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

#44
I learned this nice syntax from a tweet yesterday:

    >>> import datetime
    >>> now = datetime.datetime.now()
    >>> f"Today is {now:%m/%d/%Y}."
    'Today is 01/20/2021.'
No need for now.strftime() in simple string output!

https://twitter.com/mariatta/status/1351359518316216321

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

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

I quite like the idea of using double-quotes for format strings and single-quotes for normal strings.

This isn’t sufficient on its own, though - what about raw strings? Perhaps use “” for format strings, ‘’ for raw strings and get rid of normal strings altogether?

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

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

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

The problem with this is when you run it through a formatter that changes all ' to ".

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

#47
post #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...

Way better -> https://pypi.org/project/q/

The fact that it can only output to a single hardcoded file already proves that it is not way better in the general case.

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

#49
post #16

PHP: Am I joke to you?

> PHP: Am I joke to you? JFYI meme style responses are frowned upon at HN. That's why you get downvoted. A much better rephrase of the above would be something along the lines of: PHP/Javascript/.. also have this feature since ver X. It's main difference/advantage/disadvantage is ...

Thanks.

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

#50

This reminds me of Swift's string formatting syntax: let name = "Bob" print("Hi my name is \(name)") Which I always quite liked. I found myself missing this when I moved on to Rust and Go.

This overloads the backslash \(). Which is usually used for escaping characters. Like, \n for new line. So, you may end up with code like: print(“Hi. \nMy name is \(name).”)

And do you think that’s good or bad?

I think it’s nice that there is only one escape character. You check for ‘\’. If it’s there, switch on the next character checking for ‘n’, ‘t’, ‘r’ or ‘(‘, defaulting to appending the unmodified character to the output.

I think that might not only be simpler, but also be slightly faster than having having two different escapes would be.

Post reply on HN