I am on Python 3.7.
My code on line 3 works fine, however when I insert the underlying formula into line 4, my code returns the error:
SyntaxError: f-string: mismatched '(', '{', or '[' (the error points to the first '(' in line 4.
My code is:
cheapest_plan_point = 3122.54
phrase = format(round(cheapest_plan_point), ",")
print(f"1: {phrase}")
print(f"2: {format(round(cheapest_plan_point), ",")}")
I can't figure out what is wrong with line 4.
You used "
quotes inside a "..."
delimited string.
Python sees:
print(f"2: {format(round(cheapest_plan_point), "
,
")}")
so the )}
is a new, separate string.
Use different delimiters:
print(f"2: {format(round(cheapest_plan_point), ',')}")
However, you don't need to use format()
here. In an f-string, you already are formatting each interpolated value! Just add :,
to apply the formatting instruction directly to the round()
result:
print(f"2: {round(cheapest_plan_point):,}")
The format {expression:formatting_spec}
applies formatting_spec
to the outcome of expression
, just as if you used {format(expression, 'formatting_spec')}
, but without having to call format()
and without having to put the formatting_spec
part in quotes.