3

I have the following code:

def bear_room():
    print("""There is a bear here
    The bear has a bunch of honey
    The fat bear is in front of another door
    How are you going to move the bear?
    """)

And it returns the following:

There is a bear here
    The bear has a bunch of honey
    The fat bear is in front of another door
    How are you going to move the bear?

Can anyone advise how I can get rid of the indentation on lines 2, 3, and 4?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

4 Answers4

2

If you can't modify that string literal (for example, it's defined outside of your code), use inspect.cleandoc or equivalent:

In [2]: import inspect

In [3]: s = inspect.cleandoc("""There is a bear here
   ...:     The bear has a bunch of honey
   ...:     The fat bear is in front of another door
   ...:     How are you going to move the bear?
   ...: """)

In [4]: print(s)
There is a bear here
The bear has a bunch of honey
The fat bear is in front of another door
How are you going to move the bear?

If you can modify it, it's much easier to remove leading spaces manually:

def bear_room():
    print("""There is a bear here
The bear has a bunch of honey
The fat bear is in front of another door
How are you going to move the bear?
""")

or use implicit string literal concatenation:

def bear_room():
    print('There is a bear here\n'
          'The bear has a bunch of honey\n'
          'The fat bear is in front of another door\n'
          'How are you going to move the bear?\n')

This option will produce expected results if you end strings with newline characters (\n).

vaultah
  • 44,105
  • 12
  • 114
  • 143
2

Please check this answer: https://stackoverflow.com/a/2504457/1869597

One of your options is to use implicit concatenation:

def bear_room():
    print(
        "There is a bear here\n"
        "The bear has a bunch of honey\n"
        "The fat bear is in front of another door\n"
        "How are you going to move the bear?"
    )
Jordan Jambazov
  • 3,460
  • 1
  • 19
  • 40
2

One can use the dedent function. The following solution has the 3 advantages

  • one does not have to add \n character at the end of each line, the text can directly be copied from extern source,
  • one can keep the indentation of our function,
  • no extra line are inserted before and after of the text

The function looks like this:

from textwrap import dedent
dedentString = lambda s : dedent(s[1:])[:-1]

def bear_room():
    s="""
    There is a bear here
    The bear has a bunch of honey
    The fat bear is in front of another door
    How are you going to move the bear?
    """
    print("Start of string : ")
    print(dedentString(s))
    print("End of string")

bear_room()

The result is :

Start of string :
There is a bear here
The bear has a bunch of honey
The fat bear is in front of another door
How are you going to move the bear?
End of string
Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
0

In your original code, after the first line, there is indentation - the indentation inside the triple quoted string is whitespace. So you need to remove those.

The following works:

def bear_room():
    print("""There is a bear here
The bear has a bunch of honey
The fat bear is in front of another door
How are you going to move the bear?""")

bear_room()
masnun
  • 11,635
  • 4
  • 39
  • 50
  • Yes, that also works! However for the purpose of keeping the code tidy, I would stick with Jordan's answer above. Because all the code under 'print' resides within the 'bear_room' function. Thanks. – SnakeInTheGrass Jan 03 '16 at 17:52
  • Sure, if it helps, you can upvote more than one answers. – masnun Jan 03 '16 at 17:54