在f字符串插值中转义引号

0__*_*0__ 1 scala string-interpolation

我无法在以下字符串插补器中包含引号:

f"foo ${math.abs(-0.9876f)*100}%1.1f%%"
Run Code Online (Sandbox Code Playgroud)

输出是

foo 98.8%
Run Code Online (Sandbox Code Playgroud)

现在所需的输出是

foo "98.8%"
Run Code Online (Sandbox Code Playgroud)

插入\"不起作用,只会产生"未关闭的字符串文字"错误.

ten*_*shi 10

似乎这个问题不会得到解决.您可以使用以下解决方法之一:

  1. 多行字符串:

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