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
  • 85,390
  • 12
  • 140
  • 213
forvaidya
  • 2,685
  • 3
  • 20
  • 27

3 Answers3

8

Use triple single quotes:

'''
"""Test"""
'''
BrenBarn
  • 228,001
  • 34
  • 392
  • 371
1

You can also try,

>>> a = "\"\"\"TEST\"\"\""
>>> print a
"""TEST"""
>>> 
Fury
  • 287
  • 3
  • 7
Nishant Nawarkhede
  • 7,757
  • 11
  • 56
  • 79
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
  • 85,390
  • 12
  • 140
  • 213