4

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.

black_bird
  • 115
  • 1
  • 5
  • 12
  • See also: [How to write string literals in python without having to escape them?](https://stackoverflow.com/questions/4703516/); [How to include a quote in a raw Python string](https://stackoverflow.com/questions/4630465/) – Karl Knechtel Aug 07 '22 at 08:03

3 Answers3

6

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 """ \""" """
 """
>>>
  • Worth noting: you will run into problems if your string happens to contain triple quotes, which might happen if you are using strings like this to hold ASCII art. (In that case, you must escape the quote characters.) – nneonneo Jun 17 '14 at 22:28
  • @nneonneo I just happened to read about raw strings in this post: http://stackoverflow.com/questions/647769/why-cant-pythons-raw-string-literals-end-with-a-single-backslash?rq=1 however, they mention that it permanently alters the string with the addition of a backslash...unless I misread. Is that the only other way to interpret this type of input? – black_bird Jun 17 '14 at 22:34
  • @black_bird: "permanently alters"? I don't think I understand. Use raw strings if, and only if, you want to have lots of raw backslashes in your string *and* you are not interested in using escape characters at all. Regular expressions are one of the few cases in which you actually want all of that; for everything else, there's plain strings. – nneonneo Jun 17 '14 at 22:47
5

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:

           ./'..|'.|| |||||\```````  "  '''''''/||||| ||.`|..`\.
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
0

Just a side note

This does not answer the question. Just saying: Check whether you need an answer to the question above at all.

For example in SQL when running a query from Python, you might think that you need to prepare the string values that are passed since they might include special signs. Instead, you do not need to care, and you should not care, see Python MySQL escape special characters, and it might rather be the new aim to avoid the automatic escapes (How to prevent automatic escaping of special characters in Python which is then at the r topic again). If possible, you should leave it to a library function to deal with the special characters and pass your string(s) as args (arguments).

That way, you can just deal with the string as a string, and let the MySQL library figure out how to quote and escape it for you.

questionto42
  • 7,175
  • 4
  • 57
  • 90