When to use triple single quotes instead of triple double quotes

前端 未结 2 1489
一生所求
一生所求 2021-01-31 02:36

Learn Python the hard way, exercise 10.2:

tabby_cat = \"\\tI\'m tabbed in.\"
persian_cat = \"I\'m split\\non a line.\"
backslash_cat = \"I\'m \\\\ a \\\\ cat.\"
         


        
2条回答
  •  时光取名叫无心
    2021-01-31 02:59

    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.

提交回复
热议问题