Search code examples
pythonstringescaping

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?

I am trying to print this line for some fun ASCII in a program and this is just one of the lines I'm getting errors on due to intermediary quotation marks popping up:

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

Edit: While considering the raw interpretation of string literals, you can also run into the triple-quoted exit within the raw interpretation should the triple quote appear in your line.


Solution

  • 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:

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