When to use triple single quotes instead of triple double quotes

前端 未结 2 1490
一生所求
一生所求 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:57

    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'""")
    

提交回复
热议问题