python中的引號

        學過python的朋友應該都知道,python中包含單引號,雙引號和三引號。但是他們有什麼區別呢?

        其實,我個人感覺,python不同於php,其中的單引號和雙引號沒有區別。他們的主要作用就是儘量避免使用轉義字符。例如:

>>> a='I\'m a student.'
>>> print a
I'm a student.
>>> a="I'm a student."
>>> print a
I'm a student.
>>> b="\"Knuth\" is a genius."
>>> print b
"Knuth" is a genius.
>>> b='"Knuth" is a genius'
>>> print b
"Knuth" is a genius

      至於三引號,其主要目的是用來輸入多行文本,也就是說在三引號之間輸入的內容將被原樣保留,之中的單號和雙引號不用轉義,其中的不可見字符比如\n和\t都會被保留,這樣的好處是你可以替換一些多行的文本。

c='''The Zen of Python, by Tim Peters 
    
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!''' 
print c
         打印結果就不粘了,大家自己回去試一試吧。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章