13
votes

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.

1
You can use multi-line strings: f"""foo "${math.abs(-0.9876f)*100}%1.1f%"""" - tenshi
Ah right. I thought that was restricted to the raw method, didn't know these were independent. thanks - 0__
If you post that as answer, I can accept and close - 0__
The following does not work, any help: line = f'list(\'\')\n'. The single quoation will not be escaped and not shown. - Timo

1 Answers

21
votes

Seems that this problem wouldn't be fixed. You can use one of the following workarounds:

  1. multi-line strings:

    f"""foo "${math.abs(-0.9876f)*100}%1.1f%""""

  2. \042:

    f"foo \042${math.abs(-0.9876f)*100}%1.1f%\042"

  3. ${'"'}:

    f"foo ${'"'}${math.abs(-0.9876f)*100}%1.1f%${'"'}"