>>> x = 1
>>> f"{x}"
'1'
This is much more readable than: >>> "{x}".format(x=x)
'1'
>>> "{}".format(x)
'1'
It makes the code immensely more readable than having to count parameters, especially for long strings with a lot of data in them.
Counting is for computers, not programmers; I shouldn't have to count anything for such a trivial task.So, thanks to f"", no need for what I find to be an ugly additional call to .format(...), that is just visual clutter for most cases.
The only reasons I could see for a call with a dedicated dict is data exfiltration from a user provided format string executed on python code they do not control, or renaming/subsetting of keywords for cleanliness. Both are special cases, and that's the effect the current implementation has, thankfully.
I've heard similar complaints about the loop macro in Common Lisp, and this feels a bit like shell or Tcl strings which could mean anything (arguments to sed/awk or paths to widgets/urls). However, I guess people are familiar with regular expressions as a completely foreign nested language (DSL), so this isn't much different than that.
No its not.
"My name is {}, I'm {}'{}'' tall and live in {}".format(name, location, feet, inches)
This error is much better hidden than in the equivalent: f"My name is {name}, I'm {location}'{feet}'' tall and live in {inches}."I think this is more explicit:
"My name is {name}, I'm {feet} tall and live in {location}.".format(name="Bob", location="USA", feet=7)
Your actual example would be:
"My name is {name}, I'm {feet} tall and live in {location}.".format(name=name, location=location, feet=feet)
And with f-strings: f"My name is {name}, I'm {feet} tall and live in {location}."
f-strings is clearly more readable, concise and actually faster to run.If its faster to run, then fair enough.
"My name is {name}, I'm {feet} tall and live in {location}.".format(name="Bob", location="USA", feet=7)
is less explicit than
"My name is Bob, I'm 7 feet tall and live in USA."
F"My string with {interpolated_value}"
f'My string with {interpolated_value}'
Like using apostrophes or quotation marks to enclose strings (or F-strings), I find myself wasting time questioning the trivial notion of whether to capitalize or not.I think Groovy's syntax is great, where quotation marks denote an interpolated string, and apostrophes denote a plain old string.
"This is my ${interpolated_value} string"
'This is my plain old string'
But these are nitpicks, and it's really too late to implement something like that.That's Perl's syntax, from about 15 years before Groovy, whippersnapper. Off my lawn, now.
Oh, although I guess Perl got that syntax from the Bourne shell…
Huh, I could have sworn that I remembered that Groovy has taken it from Ruby.
Incidentally, Ruby uses just # for variables, it uses #{} for arbitrary expressions. A common style prefers #{} even where # alone works, though.
An early beta version of Groovy 1.0 had it (thanks to a Sam Pullara), but it was later yanked out. Groovy's self-styled Project Manager at the time said he only wanted syntax in Groovy that would cause the Java syntax highlight rules in Eclipse and Netbeans to highlight Groovy code similar to Java, so if a manager was walking around the programming area, the screens would look like the programmers were using Java.
And that's how Groovy got its deficient string syntax.