# Markdown code-block style syntax for string literals

**URL:** <https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679>\
**Category:** Ideas\
**Created:** [December 9, 2023, 6:51am UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679 "2023-12-09T06:51:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![absop](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/absop/32/16356_2.png) [@absop](https://discuss.python.org/u/absop)\
**Post date:** [December 9, 2023, 6:51am UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/1 "2023-12-09T06:51:14Z")

</div>

I think a `Markdown` code-block style syntax is pretty useful, with that, we can event highlight the multi-line str areas as code of some programming languages.

In this syntax, the word or sentence on the same line immediately after the beginning “```”, whose function is to identify the type or role of the string, will not be included in the resulting string.

---

<div class="post-metadata">

**Author:** ![gpshead](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gpshead/32/54_2.png) [@gpshead](https://discuss.python.org/u/gpshead)\
**Post date:** [December 9, 2023, 7:08pm UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/2 "2023-12-09T19:08:34Z")

</div>

The biggest downside to ``` as a string syntax, and reason I expect that never to be used within _any_ programming language at this point: No code examples within a Markdown document would be able to contain triple-backtick tokens for strings. That’d be awkward.

I presume the reason Markdown chose to use triple-backticks it is that it was not used within any programming language. Markdown’s wide popularity in effect means it has become a Markdown only concept.

Otherwise while the idea is interesting, Python adopted the letter-prefix on quotes style (`r""` `b""` `f""` universally across all four `"`, `'`, `"""`, `'''` styles) for string modifiers long ago. Choosing a new way of doing this would be inconsistent.

---

<div class="post-metadata">

**Author:** ![stoneleaf](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stoneleaf/32/88_2.png) [@stoneleaf](https://discuss.python.org/u/stoneleaf)\
**Post date:** [December 9, 2023, 7:46pm UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/3 "2023-12-09T19:46:39Z")

</div>

I’m not clear on exactly what the proposal is, but just as a point of reference, three tildes (`~~~`) are also accepted as code block delimiters.

---

<div class="post-metadata">

**Author:** ![absop](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/absop/32/16356_2.png) [@absop](https://discuss.python.org/u/absop)\
**Post date:** [December 9, 2023, 8:05pm UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/4 "2023-12-09T20:05:00Z")

</div>

It’s ok to change ````` to `"""`, but a syntax that automatically dedent multi-line strings and ignore the words that follow on the same line as the beginning delimiter would be useful.

Example:

```Python
def query_students(name, age):
    code = fc"""SQL # Pretend there's a comment here
    SELECT *
    FROM student
    WHERE name = "{name}"
      AND age = {age}
    """
    ...

```

Before doing a string formatting, the string literal above would be treated as `"SELECT *\nFROM student\nWHERE name = \"{name}\"\n AND age = {age}\n"`.

---

<div class="post-metadata">

**Author:** ![abatea](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abatea/32/16368_2.png) [@abatea](https://discuss.python.org/u/abatea)\
**Post date:** [December 9, 2023, 8:22pm UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/5 "2023-12-09T20:22:11Z")

</div>

This is interesting to me. I’ve long wondered why multi-line strings aren’t automatically dedented just like docstrings. “”" literals (especially f"“”) are widely useful, and universally awkward when they don’t have correct-looking indentation compared to the surrounding code.

---

<div class="post-metadata">

**Author:** ![gpshead](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gpshead/32/54_2.png) [@gpshead](https://discuss.python.org/u/gpshead)\
**Post date:** [December 9, 2023, 8:40pm UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/6 "2023-12-09T20:40:59Z")

</div>

> [@abatea](#):
>
> I’ve long wondered why multi-line strings aren’t automatically dedented just like docstrings.

For that, see the original [Str.dedent vs str.removeindent - #34](https://discuss.python.org/t/str-dedent-vs-str-removeindent/30127/34) for that (and links to other discussions and related feature requests).

---

<div class="post-metadata">

**Author:** ![loic-simon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/loic-simon/32/37461_2.png) [@loic-simon](https://discuss.python.org/u/loic-simon)\
**Post date:** [December 9, 2023, 9:02pm UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/7 "2023-12-09T21:02:23Z")

</div>

This somehow relates with the recent idea of using _annotations for common string formats and embedded language snippets_ ([python/typing#1370](https://github.com/python/typing/issues/1370#issuecomment-1512495834), [typing\_extensions#55](https://github.com/annotated-types/annotated-types/issues/55)). That would allow to write the above example as

```auto
def query_students(name, age):
    code: Annotated[str, Language["sql"]] = f"""
    SELECT *
    FROM student
    WHERE name = "{name}"
      AND age = {age}
    """
    ...

```

---

<div class="post-metadata">

**Author:** ![kknechtel](https://avatars.discourse-cdn.com/v4/letter/k/e47c2d/32.png) [@kknechtel](https://discuss.python.org/u/kknechtel)\
**Post date:** [December 9, 2023, 11:46pm UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/8 "2023-12-09T23:46:08Z")

</div>

> [@gpshead](#):
>
> For that, see the original [Str.dedent vs str.removeindent - #34](https://discuss.python.org/t/str-dedent-vs-str-removeindent/30127/34) for that (and links to other discussions and related feature requests).

Ah, this is my fault.

The OP of this thread was originally in that thread - I flagged it as off-topic, because I couldn’t make sense of it except that it seemed to be proposing something only vaguely related. But now I understand that it was intended as a different design for the same thing.

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [December 10, 2023, 3:44am UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/9 "2023-12-10T03:44:42Z")

</div>

Well, it would be my fault too, of course. Should I merge it back in, then?

---

<div class="post-metadata">

**Author:** ![absop](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/absop/32/16356_2.png) [@absop](https://discuss.python.org/u/absop)\
**Post date:** [December 10, 2023, 5:17am UTC](https://discuss.python.org/t/markdown-code-block-style-syntax-for-string-literals/40679/10 "2023-12-10T05:17:12Z")

</div>

I don’t like this approach very much because it’s complicated for learner, and it doesn’t indicate that this is a string that the compiler will automatically unindent.

The main problem is that it is very difficult (and almost impossible) for editors (not IDEs, such as SublimeText, CodeMirror) or code highlighters (such as Prismjs) to support it.

I think it’s helpful to have a string syntax that automatically discards anything on the same line as the beginning delimiter, because the first line is special and bad for alignment, we can use it as a comment.
