寫Python程序的同學(xué)經(jīng)常碰到這幾個(gè)符號(hào),它們之間的區(qū)別是什么呢?
這幾個(gè)符號(hào)都是可以表示字符串的,如果是表示一行,則用單引號(hào)或者雙引號(hào)表示,它們的區(qū)別是如果內(nèi)容里有"符號(hào),并且你用雙引號(hào)表示的話則需要轉(zhuǎn)義字符,而單引號(hào)則不需要。
三單引號(hào)和三雙引號(hào)也是表示字符串,并且可以表示多行,遵循的是所見即所得的原則。
另外,三雙引號(hào)和三單引號(hào)可以作為多行注釋來用,單行注釋用#號(hào)。
下面一個(gè)簡(jiǎn)單的示例程序就可以說明:
#coding=utf-8
'''
This is multilie comment
Bla, Bla
'''
s1 = 'Hello, world ' \
'How are you?'
s2 = '''
This is a test. "
Enjoy it!
'''
s3 = """
This is a test. "
Enjoy it!
"""
s4 = 'messi"'
s5 = "messi\""
print s1
print s2
print s3
print s4
print s5