# Trimmed multiline string

**URL:** <https://discuss.python.org/t/trimmed-multiline-string/2600>\
**Category:** Ideas\
**Created:** [November 5, 2019, 10:45pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600 "2019-11-05T22:45:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 5, 2019, 10:45pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/1 "2019-11-05T22:45:12Z")

</div>

Usually, if I have to write a multiline string, I have do:

```python
a = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

Donec enim diam vulputate ut pharetra sit amet. 

Vitae tortor condimentum lacinia quis."""

```

I propose to add another multiline string: `t"""multiline string"""`

With this type, the previous multine can be written as:

```python
a = """
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
    incididunt ut labore et dolore magna aliqua. 

    Donec enim diam vulputate ut pharetra sit amet. 

    Vitae tortor condimentum lacinia quis.
"""

```

A `t"""multiline string"""` should act as a normal multiline string, with this exceptions:

1. one newline character at the start and at the end of the string will be removed, if present
2. every line after the first one will be right left trimmed

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [November 6, 2019, 4:02am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/2 "2019-11-06T04:02:44Z")

</div>

Do you mean left trimmed instead of right? With right trimming you’d remove the new line but keep the indentation.

---

<div class="post-metadata">

**Author:** ![brandtbucher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brandtbucher/32/22862_2.png) [@brandtbucher](https://discuss.python.org/u/brandtbucher)\
**Post date:** [November 6, 2019, 5:17am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/3 "2019-11-06T05:17:25Z")

</div>

This already exists. Just call `inspect.cleandoc(a)`. It gives it the [PEP 257](https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation) docstring treatment (for example, what the built-in `help` utility shows you).

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [November 6, 2019, 7:27am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/4 "2019-11-06T07:27:05Z")

</div>

The different is similar to f-string vs `str.format()`. The latter exists and solves the problem, but incurs runtime penalty, and is arguably less readable. Of course, formatting is a much more common operation, and doesn’t have a feasible alternative other than f-string, so this may not be useful enough to warrant a dedicated syntax. It does IMO, but that might not be the majority opinion.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [November 6, 2019, 10:02am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/5 "2019-11-06T10:02:12Z")

</div>

There was several discussions about this at last few years. Please find and read the past discussions before opening a new discussion and repeating old arguments which already was considered many times.

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 6, 2019, 10:31am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/6 "2019-11-06T10:31:34Z")

</div>

> this may not be useful enough to warrant a dedicated syntax

Well, it can only be applied to all docstrings… 😃

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 6, 2019, 10:32am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/7 "2019-11-06T10:32:52Z")

</div>

> There was several discussions about this at last few years

You’re right, but I read them and no one proposed this syntax.

@uranusjr: Anyway, since it was proposed many times and also a PEP was opened about it, I think _ **is** _ useful enough.

---

<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:** [November 6, 2019, 8:24pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/8 "2019-11-06T20:24:03Z")

</div>

Yes, this has come up before. - It is tracked as [https://bugs.python.org/issue36906](https://bugs.python.org/issue36906) - I’m in favor of solving it at the language level.

`inspect.cleandoc()` and `textwrap.dedent()` are not solutions. They have a run-time cost _and_ the process is forced to carry the burden of the extra padded string data around with it in memory.

I’d like it if all docstrings were treated this way by default (without a prefix) but I’m assuming there are objections to a context dependent semantic change. Practicality beats purity is my response even if I don’t expect to win that one.

Options for dedicated syntax (the actual desire):

1. Add yet another letter prefix such as `t` or `m` or `d` or `c` (and argue about which letter).

2. Reuse the backquote (backtick) character now that we have removed it as a token and feature in Python 3. We could introduce a ````` token that acts as an auto-cleandoc’d or dedent’d multi-line string. (the other letter prefixes would apply to it). I intentionally recommend not supporting a single backquote, only a new triple tick token. That way it avoids python 2 ` repr vs python 3 syntax concerns.

A drawback to all options: Yet more new syntax.

**Drawback on #1** : The letter prefix makes no sense on a single quote string, but we’ve not differentiated between single or triple quotes when it comes to prefixes in the past. It’d be a no-op on single line string literals unless we decided to break that rule.

**Drawback on #2** : An sad downside to ````` is that it is virtually impossible to represent within most markdown documentation rendering engines. And markdown is crazy-popular. They don’t have a way - or don’t have a consistent way - to escape a triple backquote within the middle of their own multi-line. 😕

````auto
>>> def castle_argh(self):
... return ```multi-line
... 
... dedented string.
... ```
... 
>>> castle_argh()
'multi-line\n\ndedented string\n'

````

That only renders in here Discourse _today_ because i added the REPL prefixes on the line. I can’t write a cut and pasteable code snippet using those. `<pre>` tags appear to work _here_, but lose syntax highlighting. And there seem to be 183 markdown renderer implementations out there (Github itself applies multiple _to the same files_!), each of which varies its behavior between one another and over time.

**Upside to #2** : People are already used to reading ````` as a multi-line block thanks to markdown! 🙂

Example implementations in PRs along with a PEP laying this out would help.

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 6, 2019, 8:42pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/9 "2019-11-06T20:42:22Z")

</div>

Well, I’m not against `````, but it remembers to me more the start of a code block instead of a text block.

About letter, I’m in favor of `t` for `trim`. And yes, I don’t like the fact that is useful for multi-line strings only. Maybe the optimal solution is another one.

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [November 6, 2019, 8:56pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/10 "2019-11-06T20:56:19Z")

</div>

Third option – automatically dedent all string literals. Overwhelming majority of multiline string literals should be dedented. It is easy to make it by default and add a way to prohibot dedenting.

This is backward incompatible change, so we will need to introduce a future import for switching this behavior.

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 6, 2019, 9:00pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/11 "2019-11-06T21:00:43Z")

</div>

-1 to any backward incompatibility. Py2-\>Py3 is not enough?

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [November 7, 2019, 7:20am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/12 "2019-11-07T07:20:49Z")

</div>

> [@gpshead](#):
>
> **Drawback on #2** : An sad downside to ````` is that it is virtually impossible to represent within most markdown documentation rendering engines. And markdown is crazy-popular. They don’t have a way - or don’t have a consistent way - to escape a triple backquote within the middle of their own multi-line.

Most Markdown implementations have _two_ code block delimiters, triple backtick (```) and triple tilde (`~`), but for some reason most people seem to be unaware of the latter. You need no additional escaping if you wrap the code block with `~~~` instead of ````` 🙂

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 7, 2019, 10:04am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/13 "2019-11-07T10:04:51Z")

</div>

Most. And users usually wants to do a copy - paste simply. And furthermore ````` means _ **code** _ block, in Markdown, not _ **text** _ block.

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [November 7, 2019, 10:40am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/14 "2019-11-07T10:40:32Z")

</div>

I challenge you to actually find a meaningful Markdown implementation that tilde does not work 😬

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 7, 2019, 11:50am UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/15 "2019-11-07T11:50:17Z")

</div>

I challenge you to find a not lazy programmer that do not do only copy - paste and know about `~~~`

---

<div class="post-metadata">

**Author:** ![brandtbucher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brandtbucher/32/22862_2.png) [@brandtbucher](https://discuss.python.org/u/brandtbucher)\
**Post date:** [November 7, 2019, 3:05pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/16 "2019-11-07T15:05:24Z")

</div>

> Third option – automatically dedent all string literals. Overwhelming majority of multiline string literals should be dedented. It is easy to make it by default and add a way to prohibot dedenting.

Hmm. If you want to keep whitespace, just use a `r"raw"` string then?

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 7, 2019, 3:19pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/17 "2019-11-07T15:19:57Z")

</div>

I think it intended multi-strings only. But yes, I think you have to put `r"""blablabla"""`.

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 7, 2019, 3:28pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/18 "2019-11-07T15:28:54Z")

</div>

Mh, well, no. The original propose of @storchaka was [this one](https://bugs.python.org/issue36906#msg342931). I think that `r` makes much more sense…

Anyway, for what it’s worth, I continue to be against something that break also a 0.000000000000000000000000000000000000000000000000000000000000000001% of the already written code.

---

<div class="post-metadata">

**Author:** ![Marco\_Sulla](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/marco_sulla/32/1451_2.png) [@Marco\_Sulla](https://discuss.python.org/u/Marco_Sulla)\
**Post date:** [November 7, 2019, 3:51pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/19 "2019-11-07T15:51:14Z")

</div>

Excuse me for the spam. @gpshead: do you think I can, and it’s worth it, to write a PEP about this?

---

<div class="post-metadata">

**Author:** ![storchaka](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/storchaka/32/217_2.png) [@storchaka](https://discuss.python.org/u/storchaka)\
**Post date:** [November 7, 2019, 4:50pm UTC](https://discuss.python.org/t/trimmed-multiline-string/2600/20 "2019-11-07T16:50:45Z")

</div>

> [@brandtbucher](#):
>
> If you want to keep whitespace, just use a `r"raw"` string then?

No, the `r` prefix affects the interpretation of a backslash.

I proposed to add non-indented `\<newline>` if you want to keep indentation:

```python
a = """\
    dedented
"""
b = """\
\
    not dedented
"""

```

Other option – always include the line containing the closing `"""`:

```python
a = """\
    dedented
    """
b = """\
    not dedented
"""

```

It corresponds [Julia syntax](https://docs.julialang.org/en/v1/manual/strings/#Triple-Quoted-String-Literals-1) which looks well thought out.

[Next page](https://discuss.python.org/t/trimmed-multiline-string/2600.md?page=2)
