You can escape quotes with \'
and \"
, or you can use triple quotes:
example1 = 'She\'s fond of saying "Hello"'
example2 = "She's fond of saying \"Hello\""
example3 = '''She's fond of saying "Hello"'''
example4 = """She's fond of saying "Hello\""""
Be careful with the closing "
at the end in the triple-quoted string; it has to be escaped still to not count for the last 3 closing quotes.
Demo:
>>> example1 = 'She\'s fond of saying "Hello"'
>>> example2 = "She's fond of saying \"Hello\""
>>> example3 = '''She's fond of saying "Hello"'''
>>> example4 = """She's fond of saying "Hello\""""
>>> example1 == example2 == example3 == example4
True
In addition, newlines are permitted and preserved in a triple-quoted string, making for a very readable literal HTML value:
html = '''\
<acronym class="non-experimental-qualifier"
title="Indicates that the information given is not based on experimental findings."
onclick="dialog('non_experimental_qualifiers'); Event.stop(event || window.event); return false"
onmousedown="Event.stop(event || window.event);">By similarity</acronym>.
<a class="attribution" href="http://hamap.expasy.org/unirule/'
'''