-1

I am using triple quotes to create a multiline string:

print(
    ''' Hello!
        How are
        you?''')

Output:

 Hello!
        How are
        you?

Inside the string, How is coming exactly underneath Hello.

However, I am not able to understand what is causing the indentation to change.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
meallhour
  • 13,921
  • 21
  • 60
  • 117
  • 5
    It's **not** changing it. That's the same indentation as inside the string. – jonrsharpe Oct 13 '21 at 21:08
  • But inside the string, `How` is coming exactly underneath `Hello`. – meallhour Oct 13 '21 at 21:10
  • 3
    Sure, but the characters on the line before it are mostly _not_ in the string. – jonrsharpe Oct 13 '21 at 21:12
  • 2
    In your string you have Hello! then a newline, and then 8 spaces, then How are. That's what it's printing. If you don't want those 8 spaces printing, take them out of your string literal. – khelwood Oct 13 '21 at 21:12
  • Proposal to add a new string prefix to make this stuff easier: https://discuss.python.org/t/indented-multi-line-string-literals/9846 – wim Oct 13 '21 at 21:25

5 Answers5

3

To answer the question, the number of spaces inside the string are exactly correct, triple quoting is not changing indentation.

To answer the question behind the question, use textwrap.dedent if you want to remove common leading whitespace so that multiline strings still line up nicely in the source code:

>>> print(dedent('''\
...           Hello!
...           How are
...           you?'''))
Hello!
How are
you?

You may be interested also to follow Compile time textwrap.dedent() equivalent for str or bytes literals.

wim
  • 338,267
  • 99
  • 616
  • 750
2

When you use triple quotes like this, the resulting string will contain all of the characters between the triple quotes, including spaces and line breaks.

So, let's look at your code again. It looks like this:

    ''' Hello!
        How are
        you?'''

If we look closely at this code, we can see that it consists of:

  • 4 spaces
  • The triple quotation mark '''
  • 1 space
  • The word Hello!
  • A line break
  • 8 spaces
  • The words How are
  • A line break
  • 8 spaces
  • The word you?
  • The triple quotation mark '''

Therefore, the resulting string will contain all of the characters between the triple quotes, namely:

  • 1 space
  • The word Hello!
  • A line break
  • 8 spaces
  • The words How are
  • A line break
  • 8 spaces
  • The word you?

And as you can see, that's exactly what got printed out.

Tanner Swett
  • 3,241
  • 1
  • 26
  • 32
1

Let's look at the Python docs:

Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points. String literals are written in a variety of ways:

Single quotes: 'allows embedded "double" quotes'

Double quotes: "allows embedded 'single' quotes".

Triple quoted: '''Three single quotes''', """Three double quotes"""

Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.

I'm not sure what your desired output is, but the short answer is that Python interprets whitespace inside triple-quotes literally; that's why it's included in the printed output.

whege
  • 1,391
  • 1
  • 5
  • 13
0

Triple quotes represent exactly what you write as shown below:

<No spaces and no indentations>:

test = """<= No spaces with no indentatoins
<= No spaces with no indentatoins
"""

print(test)

Output:

<= No spaces with no indentatoins
<= No spaces with no indentatoins

<Some spaces and some indentations>:

test = """  
  <= 2 spaces with 1 indentation
    
    
    <= 4 spaces with 2 indentations
"""
       
print(test)

Output:

  
  <= 2 Spaces with 1 indentation
    
    
    <= 4 spaces with 2 indentations

Somehow, triple quotes are similar to <pre></pre> in HTML as shown below:

<pre>
<= No spaces with no indentatoins
<= No spaces with no indentatoins
</pre>

<pre>

  <= 2 Spaces with 1 indentation


    <= 4 spaces with 2 indentations
</pre>
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
-4

try this

print(f''' 
Hello!
How are
you?''')
Cooper
  • 1
  • 1
  • 6
    Why is that an f-string? – jonrsharpe Oct 13 '21 at 21:14
  • Good question. It doesn't have to be but I'm used to using f-strings by default unless there are certain characters like brackets that cause issues in which case I use an r-string. You can remove the f here if you like – Cooper Oct 13 '21 at 21:29
  • Why? Unless you're actually formatting something into it (and unless there's special casing in the specific implementation) that's overhead you don't need. – jonrsharpe Oct 13 '21 at 21:36