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
         打印结果就不粘了,大家自己回去试一试吧。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章