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?

4 Answers 4

3

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?" ) 
Sign up to request clarification or add additional context in comments.

3 Comments

I think you want to indent the other strings such that the first " align
Thanks Jordan..however using your code returned one long sentence. I added \n to the end of each line which did the trick. Appreciate the help.
Ah, my bad. Yes, \n should be added at the end of each line. I edited the answer.
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).

Comments

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 

1 Comment

instead of using dedunt(s[1:])[:-1] you can use dedunt(s).strip()
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() 

2 Comments

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.
Sure, if it helps, you can upvote more than one answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.