String formatting: % vs. .format vs. f-string literal
Python 2.6 introduced the str.format()
method with a slightly different syntax from the existing %
operator. Which is better and for what situations?
Python 2.6 introduced the str.format()
method with a slightly different syntax from the existing %
operator. Which is better and for what situations?
I am using template strings to generate some files and I love the conciseness of the new f-strings for this purpose, for reducing my previous template code from something like this:
Is there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %)
I’m using the .format()
a lot in my Python 3.5 projects, but I’m afraid that it will be deprecated during the next Python versions because of f-strings, the new kind of string literal.
I have a string in which I would like curly-brackets, but also take advantage of the f-strings feature. Is there some syntax that works for this? Here are two ways it does not work. I would like to include the literal text {bar} as part of the string. foo = "test" fstring = f"{foo} {bar}" … Read more
I’m trying out Python 3.6. Going through new code, I stumbled upon this new syntax:
Thanks to David Beazley’s tweet, I’ve recently found out that the new Python 3.6 f-strings can also be nested:
I’m trying to write PEP-8 compliant code for a domestic project and I have a line with an f-string that is more than 80 characters long
I was reading this blog post on Python’s new f-strings and they seem really neat. However, I want to be able to load an f-string from a string or file.