When to use triple single quotes instead of triple double quotes

The only reason you might need """ instead of ''' (or vice versa) is if the string itself contains a triple quote.

s1 = '''This string contains """ so use triple-single-quotes.'''
s2 = """This string contains ''' so use triple-double-quotes."""

If a string contains both triple-single-quotes and triple-double-quotes then you will have to escape one of them, but this is an extremely rare situation.


I found similar situations need ''' instead of """ which is when a double quote symbol at the end of the string, vice versa.

Invalid syntaxes:

print("""2 feet 4 inches can be written in 2' 4"""")
print('''2 feet can be written in 2'''')

Valid syntaxes:

print('''2 feet 4 inches can be written in 2' 4"''')
print("""2 feet can be written in 2'""")

Tags:

Python

Quotes