问题
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