简体   繁体   中英

Avoiding escaping double quotes in a string

Is there a component of Python that allows me to bypass intermediate quotation marks? As in, can you dictate the master start and stop to a print call, so that everything in between the master start and stop is interpreted regardless of what that element originally represents?

print"           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\."
                                                                      ^
SyntaxError: EOL while scanning string literal

Why not use a triple-quoted string that has """ on each end?

>>> print """           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\."""
           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.
>>>

Note that you will still need to escape any triple quotes inside the string that match those on each end:

>>> print """ \""" """
 """
>>>

Another approach is to simply put the lines in a plaintext file and then read them in, as I would do in Linux/Unix:

$ cat > my_file.txt
           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.
^D <- control-d means end of file input from the command line

Then with Python:

with open('/path/my_file.txt') as f:
    print f.read()

should output:

           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.

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