r‘‘‘ ’’’ 與‘‘‘ ’” 有 什么區(qū)別呢
>>> print('''how are you \*_*/
... I'm fine
... and you ?''')
how are you \*_*/
I'm fine
and you ?
>>> print('\"To be, or not to be\": that is the question.\nWhether it\'s nobler in the mind to suffer.')
"To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer.
>>> print(''' To be ,or not to be :that is a question.
... Whether it's nobler int her mind to suffer.''')
?To be ,or not to be :that is a question.
Whether it's nobler int her mind to suffer.
2020-10-21
r'''...'''會(huì)對(duì)'''...'''中的內(nèi)容進(jìn)行轉(zhuǎn)義
單獨(dú)的'''...''',就只是一個(gè)換行的作用
2020-10-18
>>> print('\"To be, or not to be\": that is the question.\nWhether it\'s nobler in the mind to suffer.')
"To be, or not to be": that is the question.
Whether it's nobler in the mind to suffer.
>>> print(''' To be ,or not to be :that is a question.
... Whether it's nobler int her mind to suffer.''')
?To be ,or not to be :that is a question.
Whether it's nobler int her mind to suffer.
注意這兩個(gè)輸出的內(nèi)容有區(qū)別,第一個(gè)輸出的to be or not to be是帶有雙引號(hào)的,第二個(gè)沒(méi)有。
第一個(gè)print的內(nèi)容因?yàn)榧从须p引號(hào)"To be, or not to be",又有單引號(hào)it's,因?yàn)樵趐rint的時(shí)候需要對(duì)內(nèi)容中的引號(hào)進(jìn)行轉(zhuǎn)義,使用轉(zhuǎn)義符號(hào)\,涉及到換行的時(shí)候使用\n
第二個(gè)print的內(nèi)容是沒(méi)有雙引號(hào),只有單引號(hào)的,這種情況下不需要對(duì)引號(hào)進(jìn)行轉(zhuǎn)義,就可以輸出,只需要使用'''...'''來(lái)進(jìn)行轉(zhuǎn)行。如果第二個(gè)print的內(nèi)容跟第一個(gè)一樣,即有雙引號(hào)又有單引號(hào),那么你只使用'''...'''恐怕打印不出想要的內(nèi)容
由于轉(zhuǎn)義符號(hào)用起來(lái)比較麻煩,我們有一個(gè)相對(duì)簡(jiǎn)單的工具,叫raw字符串,即r'...',它表示對(duì)引號(hào)內(nèi)的內(nèi)容進(jìn)行轉(zhuǎn)義,即告訴計(jì)算機(jī),你甭管引號(hào)里是什么內(nèi)容了,直接依原樣輸出為字符串。當(dāng)然raw字符串也可以搭配'''...'''來(lái)使用,即多增加了一個(gè)轉(zhuǎn)行的效果。r'''...'''的作用是對(duì)引號(hào)內(nèi)的內(nèi)容進(jìn)行轉(zhuǎn)行并轉(zhuǎn)義。
'''...'''的作用只是對(duì)內(nèi)容進(jìn)行轉(zhuǎn)行,如果涉及到需要轉(zhuǎn)義的情況,它是完不成的。