简体   繁体   中英

Python triple quote returning indented string

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?

If you can't modify that string literal (for example, it's defined outside of your code), useinspect.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 ).

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?"
    )

One can use thededent 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

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM