16

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.

0__
  • 65,549
  • 20
  • 165
  • 257
  • 5
    You can use multi-line strings: `f"""foo "${math.abs(-0.9876f)*100}%1.1f%""""` – tenshi Jun 13 '13 at 11:02
  • Ah right. I thought that was restricted to the `raw` method, didn't know these were independent. thanks – 0__ Jun 13 '13 at 12:07
  • If you post that as answer, I can accept and close – 0__ Jun 13 '13 at 12:38
  • Possible duplicate of [How to insert double quotes into String with interpolation in scala](http://stackoverflow.com/questions/21086263/how-to-insert-double-quotes-into-string-with-interpolation-in-scala) – Suma Nov 13 '16 at 20:45
  • The following does not work, any help: line = f'list(\'\')\n'. The single quoation will not be escaped and not shown. – Timo Jan 08 '21 at 19:45

1 Answers1

23

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%${'"'}"

tenshi
  • 25,628
  • 8
  • 73
  • 90