# Pythonic way of printing multi-line strings

**URL:** <https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681>\
**Category:** Python Help\
**Tags:** help\
**Created:** [October 4, 2022, 11:33am UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681 "2022-10-04T11:33:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Slurms\_MacKenzie](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/slurms_mackenzie/32/9163_2.png) [@Slurms\_MacKenzie](https://discuss.python.org/u/Slurms_MacKenzie)\
**Post date:** [October 4, 2022, 11:33am UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/1 "2022-10-04T11:33:01Z")

</div>

Hello all,

I’m looking to find out what is the proper, pythonic way of printing multi-line strings, for example a message for a programs user.

The issue I’ve come across concerns indentation; when print() is nested inside a statement such as an if-then or try-except, the call to print() will be indented. If one wishes to continue the printed string onto subsequent lines in the .py file, all lines after the first must be aligned with the left margin (i.e not indented) as any indentation will be printed to the user. Consider the following example…

```auto
if True:
    print('The Owl and the Pussy-Cat went to sea in a beautiful pea green boat\n\
    they took some honey and plenty of money wrapped up in a five pound note')

```

The result would be a second line that was indented by four places past the first line within the output of the program. Docstrings exhibit the same behaviour…

```auto
if True:
    print('''The Owl and the Pussy-Cat went to sea in a beautiful pea green boat\n
    they took some honey and plenty of money wrapped up in a five pound note''')

```

The only solutions I’ve come up with so far are either to not indent any of the lines after the first…

```auto
if True:
    print('The Owl and the Pussy-Cat went to sea in a beautiful pea green boat\n\
they took some honey and plenty of money wrapped up in a five pound note')

```

Or to replace all instances of four consecutive white-space characters with an empty string…

```auto
if True:
    print('The owl and the Pussy-Cat went to sea in a beautiful pea green boat\n\
    they took some honey and plenty of money wrapped up in a five pound note'.replace(' ', ''))

```

In BASH there is a notation that can be used to write here-documents containing tabs that the shell will ignore…

```auto
if true; then
        cat <<- _EOF_
                The Owl and the Pussy-Cat went to sea in a beautiful pea green boat
                they took some honey and plenty of money wrapped up in a five pound note
        _EOF_
fi

```

I’m pretty much looking for something like the above, but if there is a more proper or ‘pythonic’ way to do it then please let me know!

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 4, 2022, 11:58am UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/2 "2022-10-04T11:58:15Z")

</div>

Have you looked at the textwrap.dedent function? Might be what you want here.

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [October 4, 2022, 5:06pm UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/3 "2022-10-04T17:06:44Z")

</div>

Instead of escaping the newline, how about just using explicit quoting?

```python
if True:
    print('The Owl and the Pussy-Cat went to sea in a beautiful pea green boat\n'
          'they took some honey and plenty of money wrapped up in a five pound note')

```

---

<div class="post-metadata">

**Author:** ![aivarpaalberg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aivarpaalberg/32/9457_2.png) [@aivarpaalberg](https://discuss.python.org/u/aivarpaalberg)\
**Post date:** [October 4, 2022, 6:08pm UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/4 "2022-10-04T18:08:20Z")

</div>

> [@Slurms\_MacKenzie](#):
>
> I’m pretty much looking for something like the above, but if there is a more proper or ‘pythonic’ way to do it then please let me know!

When in doubt, consult [PEP8 - Style Guide for Python Code](https://peps.python.org/pep-0008/)

> The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

Something like:

```python
print(('The Owl and the Pussy-Cat went to sea in a beautiful '
       'pea green boat they took some honey and plenty of money '
       'wrapped up in a five pound note'))

```

---

<div class="post-metadata">

**Author:** ![EpicWink](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/epicwink/32/17968_2.png) [@EpicWink](https://discuss.python.org/u/EpicWink)\
**Post date:** [October 4, 2022, 10:21pm UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/5 "2022-10-04T22:21:31Z")

</div>

Also try [`inspect.cleandoc`](https://docs.python.org/3/library/inspect.html#inspect.cleandoc):

```python
print(inspect.cleandoc("""First line
                          precedes the second line.
                          The final line."""))
```

---

<div class="post-metadata">

**Author:** ![Slurms\_MacKenzie](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/slurms_mackenzie/32/9163_2.png) [@Slurms\_MacKenzie](https://discuss.python.org/u/Slurms_MacKenzie)\
**Post date:** [November 1, 2022, 2:41pm UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/6 "2022-11-01T14:41:07Z")

</div>

Thanks, so it seems this is the officially sanctioned way to do it 🙂

---

<div class="post-metadata">

**Author:** ![Slurms\_MacKenzie](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/slurms_mackenzie/32/9163_2.png) [@Slurms\_MacKenzie](https://discuss.python.org/u/Slurms_MacKenzie)\
**Post date:** [November 1, 2022, 2:42pm UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/7 "2022-11-01T14:42:11Z")

</div>

I didn’t even realise this was a feature, thanks!

---

<div class="post-metadata">

**Author:** ![cameron](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cameron/32/2658_2.png) [@cameron](https://discuss.python.org/u/cameron)\
**Post date:** [November 1, 2022, 10:21pm UTC](https://discuss.python.org/t/pythonic-way-of-printing-multi-line-strings/19681/8 "2022-11-01T22:21:48Z")

</div>

Keep in mind that PEP8 is the official style guide for the code _in  
Python’s stdlib_. So this “official” recommendation is for Python’s  
library code.

That said, most people use PEP8 as the basis for their preferred style,  
either entirely or mostly.

Cheers,  
Cameron Simpson [cs@cskk.id.au](mailto:cs@cskk.id.au)
