diff --git a/components/prism-python.js b/components/prism-python.js
index b326a5ceb2..ea1a51545a 100644
--- a/components/prism-python.js
+++ b/components/prism-python.js
@@ -3,13 +3,36 @@ Prism.languages.python = {
pattern: /(^|[^\\])#.*/,
lookbehind: true
},
+ 'string-interpolation': {
+ pattern: /(?:f|rf|fr)(?:("""|''')[\s\S]+?\1|("|')(?:\\.|(?!\2)[^\\\r\n])*\2)/i,
+ greedy: true,
+ inside: {
+ 'interpolation': {
+ // "{" Known failures
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug.
def antique(string):
- """Replace anachronistic Latin "j" with "i"."""
- return string.replace("j", "i").replace("J", "I")
-
+{
or }
f"{'}'}"
diff --git a/tests/languages/python/string-interpolation_feature.test b/tests/languages/python/string-interpolation_feature.test
new file mode 100644
index 0000000000..789cb83243
--- /dev/null
+++ b/tests/languages/python/string-interpolation_feature.test
@@ -0,0 +1,147 @@
+f'The value is {value}.'
+
+f"The value is {'4'}."
+
+f'input={value!s:#06x}'
+
+f'{{{4*10}}}'
+
+fr'x={4*10}\n'
+
+f'''{x
++1}'''
+
+f'mapping is { {a:b for (a, b) in ((1, 2), (3, 4))} }'
+
+f'{(lambda x: x*2)(3)}'
+
+----------------------------------------------------
+
+[
+ ["string-interpolation", [
+ ["string", "f'The value is "],
+ ["interpolation", [
+ ["punctuation", "{"],
+ "value",
+ ["punctuation", "}"]
+ ]],
+ ["string", ".'"]
+ ]],
+
+ ["string-interpolation", [
+ ["string", "f\"The value is "],
+ ["interpolation", [
+ ["punctuation", "{"],
+ ["string", "'4'"],
+ ["punctuation", "}"]
+ ]],
+ ["string", ".\""]
+ ]],
+
+ ["string-interpolation", [
+ ["string", "f'input="],
+ ["interpolation", [
+ ["punctuation", "{"],
+ "value",
+ ["conversion-option", "!s"],
+ ["punctuation", ":"],
+ ["format-spec", "#06x"],
+ ["punctuation", "}"]
+ ]],
+ ["string", "'"]
+ ]],
+
+ ["string-interpolation", [
+ ["string", "f'{{"],
+ ["interpolation", [
+ ["punctuation", "{"],
+ ["number", "4"],
+ ["operator", "*"],
+ ["number", "10"],
+ ["punctuation", "}"]
+ ]],
+ ["string", "}}'"]
+ ]],
+
+ ["string-interpolation", [
+ ["string", "fr'x="],
+ ["interpolation", [
+ ["punctuation", "{"],
+ ["number", "4"],
+ ["operator", "*"],
+ ["number", "10"],
+ ["punctuation", "}"]
+ ]],
+ ["string", "\\n'"]
+ ]],
+
+ ["string-interpolation", [
+ ["string", "f'''"],
+ ["interpolation", [
+ ["punctuation", "{"],
+ "x\r\n",
+ ["operator", "+"],
+ ["number", "1"],
+ ["punctuation", "}"]
+ ]],
+ ["string", "'''"]
+ ]],
+
+ ["string-interpolation", [
+ ["string", "f'mapping is "],
+ ["interpolation", [
+ ["punctuation", "{"],
+ ["punctuation", "{"],
+ "a",
+ ["punctuation", ":"],
+ "b ",
+ ["keyword", "for"],
+ ["punctuation", "("],
+ "a",
+ ["punctuation", ","],
+ " b",
+ ["punctuation", ")"],
+ ["keyword", "in"],
+ ["punctuation", "("],
+ ["punctuation", "("],
+ ["number", "1"],
+ ["punctuation", ","],
+ ["number", "2"],
+ ["punctuation", ")"],
+ ["punctuation", ","],
+ ["punctuation", "("],
+ ["number", "3"],
+ ["punctuation", ","],
+ ["number", "4"],
+ ["punctuation", ")"],
+ ["punctuation", ")"],
+ ["punctuation", "}"],
+ ["punctuation", "}"]
+ ]],
+ ["string", "'"]
+ ]],
+
+ ["string-interpolation", [
+ ["string", "f'"],
+ ["interpolation", [
+ ["punctuation", "{"],
+ ["punctuation", "("],
+ ["keyword", "lambda"],
+ " x",
+ ["punctuation", ":"],
+ " x",
+ ["operator", "*"],
+ ["number", "2"],
+ ["punctuation", ")"],
+ ["punctuation", "("],
+ ["number", "3"],
+ ["punctuation", ")"],
+ ["punctuation", "}"]
+ ]],
+ ["string", "'"]
+ ]]
+]
+
+----------------------------------------------------
+
+Checks for string interpolation.
\ No newline at end of file
diff --git a/tests/languages/python/string_feature.test b/tests/languages/python/string_feature.test
index 9e21c4d340..0bb511985a 100644
--- a/tests/languages/python/string_feature.test
+++ b/tests/languages/python/string_feature.test
@@ -4,6 +4,11 @@
'fo\'obar'
"fo\" # comment obar"
+r"\n"
+b'foo'
+rb"foo\n"
+u"foo"
+
----------------------------------------------------
[
@@ -11,7 +16,12 @@
["string", "\"fo\\\"obar\""],
["string", "''"],
["string", "'fo\\'obar'"],
- ["string", "\"fo\\\" # comment obar\""]
+ ["string", "\"fo\\\" # comment obar\""],
+
+ ["string", "r\"\\n\""],
+ ["string", "b'foo'"],
+ ["string", "rb\"foo\\n\""],
+ ["string", "u\"foo\""]
]
----------------------------------------------------