I can't get to include quotation marks in the following string interpolator:
f"foo ${math.abs(-0.9876f)*100}%1.1f%%"
The output is
foo 98.8%
Now the desired output is
foo "98.8%"
Inserting \"
doesn't work, only produces "unclosed string literal" errors.
Alternatively, you can use a backslash \ to escape the quotation marks.
The backslash character allows us to escape the single quote, so it's interpreted as the literal single quote character, and not as an end of string character. You can use the same approach to escape a double quote in a string. Copied! We use the backslash \ character to escape each double quote in the string.
“F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with f , which contains expressions inside braces.
In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces. The expressions are replaced with their values. Some examples are: >>> import datetime >>> name = 'Fred' >>> age = 50 >>> anniversary = datetime.
Seems that this problem wouldn't be fixed. You can use one of the following workarounds:
multi-line strings:
f"""foo "${math.abs(-0.9876f)*100}%1.1f%""""
\042
:
f"foo \042${math.abs(-0.9876f)*100}%1.1f%\042"
${'"'}
:
f"foo ${'"'}${math.abs(-0.9876f)*100}%1.1f%${'"'}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With