2

How can I define python multi line string containing """ (3 double quotes)

my desired output is

"""
TEST
"""

Reason: I am writing a script to create some boilerplate code.

glglgl
  • 89,107
  • 13
  • 149
  • 217
forvaidya
  • 3,041
  • 3
  • 26
  • 33

3 Answers3

8

Use triple single quotes:

'''
"""Test"""
'''
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
1

You can also try,

>>> a = "\"\"\"TEST\"\"\""
>>> print a
"""TEST"""
>>> 
Fury
  • 297
  • 3
  • 7
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81
1

The solution with ''' should work, but just for the case you need both of them in a string, you can predefine a string with one of them, such as

TRIPSING = "'''"
TRIPDOUB = '"""'

and then

MYSTRING = TRIPDOUB + "\nTEST\n" + TRIPDOUB
glglgl
  • 89,107
  • 13
  • 149
  • 217