>>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295
> Single underscores are allowed between digits and after any base specifier. Leading, trailing, or multiple underscores in a row are not allowed. >>> 1_000_000_000_000_000
1000000000000000
>>> 0x_FF_FF_FF_FF
4294967295
> Single underscores are allowed between digits and after any base specifier. Leading, trailing, or multiple underscores in a row are not allowed.>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> f'{"fstring"} and {{deferred}}'.format(deferred=1234)
'fstring and 1234'But surely i18n/deferred interpolation is a corner case, not the norm. If you need that then use what you are currently using, nothings changed.
[1]
Python 3.5.2 (default, Nov 7 2016, 11:31:36)
[GCC 6.2.1 20160830] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
>>>I'm disappointed that there's so many ways to do string formatting/templating, but I think we're all so very painfully aware of how important backward compatibility is. So for the language to gain features like this, I think it should be understood that it will also keep the old ways.
No. Readability counts. One (and only one) point from the Zen of Python does not make an argument against it.
If there's four different ways to something, then that's four different things the reader has to fully understand in order to parse someone else's code.
Sure, which is why .format() kind of sucks.
> "Hi {name}, your age is {age}".format(name=name, age=age)
You have to parse 'name' and 'age' three times to figure out whats going on. The .format() is completely superfluous in most cases. It's busywork and it's harder to parse.
I quite agree with the "one obvious way" to do something, but that should not hinder progress and improvement. f strings are an improvement and are the obvious way to do things, in the same way that '.format' was an improvement over 'str % values', and how 'str % values' is an improvement over manual concatenation.
They all still exist, but lets use f strings now and stop complaining?
Worse still, I will also need to know how they interact with each other - you can do
something = "abc"
x = f"{something}".format(something="def")
and I honestly have no idea what x will be.So yeah, I would love to "use f strings and stop complaining", but the fact that the other string interpolation methods still exist, and are still supported, is still going to be a problem.
It might help to realize the f-string is just syntactic sugar for a .format() call.
>>> formatted = str.format(template, var1='a', var2=3)
Of course, this will never happen due to the need to maintain backward compatibility.I suspect there is a strong correlation between programmers that want underscores in numeric literals, and programmers that live and breath hex.