Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 406
black_bird Avatar asked Oct 20 '25 02:10

black_bird


2 Answers

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:

           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.
like image 31
Russia Must Remove Putin Avatar answered Oct 21 '25 16:10

Russia Must Remove Putin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!