Getting started on python triple quotes

纵饮孤独 提交于 2021-02-17 03:19:48

问题


I wrote a simple triple quote print statement. See below. For the OVER lineart, it gets truncated into two different lines (when you copy paste this into the interpreter.) But, if I insert a space or any at the end of each of the lines, then it prints fine. Any idea why this behavior in python.

I am inclined to think this is due to \ and / at the end of the lines, but I cannot find a concrete reason. I tried removing them and and have some observations but would like a clear reasoning..

print(
               """
                 _____       ____       __  __   ______
                / ____|     / _  |     / |  /|  |  ____|
               | |         / / | |    / /| /| | | |___
               | |   _    / /__| |   / / |_/| | |  ___|
               | |__| |  / /   | |  / /     | | | |____
                \_____/ /_/    |_| /_/      |_| |______|

                ______   _      _   ______   _____
               /  __  \ | |    / / |  ____| |  _  \
               | |  | | | |   / /  | |___   | |_| |
               | |  | | | |  / /   |  ___|  |  _  /
               | |__| | | |_/ /    | |____  | | \ \
               \______/ |____/     |______| |_|  \_\

                """
               )

回答1:


You have \ backslash escapes in your string, one each on the last two lines as well as on the first line spelling over, all three part of the letter R. These signal to Python that you wanted to ignore the newline right after it.

Either use a space right after each \ backslash at the end of a line, double the backslashes to escape the escape, or use a raw string by prefixing the triple quote with a r:

print(
               r"""
                 _____       ____       __  __   ______
                / ____|     / _  |     / |  /|  |  ____|
               | |         / / | |    / /| /| | | |___
               | |   _    / /__| |   / / |_/| | |  ___|
               | |__| |  / /   | |  / /     | | | |____
                \_____/ /_/    |_| /_/      |_| |______|

                ______   _      _   ______   _____
               /  __  \ | |    / / |  ____| |  _  \
               | |  | | | |   / /  | |___   | |_| |
               | |  | | | |  / /   |  ___|  |  _  /
               | |__| | | |_/ /    | |____  | | \ \
               \______/ |____/     |______| |_|  \_\

                """
               )

Raw strings don't support backslash escapes, except for escaped quotes (\" and \') which would be included with the backslash.




回答2:


The problem is with \ at the end of line so you need to escape them. For that i use another backslash.

print(
           """
             _____       ____       __  __   ______
            / ____|     / _  |     / |  /|  |  ____|
           | |         / / | |    / /| /| | | |___
           | |   _    / /__| |   / / |_/| | |  ___|
           | |__| |  / /   | |  / /     | | | |____
            \_____/ /_/    |_| /_/      |_| |______|

            ______   _      _   ______   _____
           /  __  \ | |    / / |  ____| |  _  \\
           | |  | | | |   / /  | |___   | |_| |
           | |  | | | |  / /   |  ___|  |  _  /
           | |__| | | |_/ /    | |____  | | \ \\
           \______/ |____/     |______| |_|  \_\\

            """
           )


来源:https://stackoverflow.com/questions/28180511/getting-started-on-python-triple-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!