Learn Python the hard way, exercise 10.2:
练习Python很难,练习10.2:
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
2: Use '''
(triple-single-quote) instead. Can you see why you might use that instead of """
?
2:改用“''(三重单引号)。你能明白为什么你可以用它代替“”“吗?
I can't see why I might use '''
instead of """
. It gives me the same output. Can someone explain me why I would use triple-single-quote instead of triple-double-quote? What's the difference between them?
我不明白为什么我可能会使用'''而不是“”“。它给了我相同的输出。有人能解释我为什么我会使用三重单引号而不是三重双引号吗?有什么区别?他们?
1 个解决方案
#1
65
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.
如果一个字符串包含三个单引号和三个双引号,那么你将不得不逃避其中一个,但这是一种非常罕见的情况。
#1
65
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.
如果一个字符串包含三个单引号和三个双引号,那么你将不得不逃避其中一个,但这是一种非常罕见的情况。