在python中一对单引号,一对双引号,三个单双引号的区别和用法

首先说明,在python中三个单双引号并不是真正的注释

>>> type("""abcde""")
<class 'str'>
>>> type('''abcd''')
<class 'str'>

这样可以看出三对单,双引号是有数据类型的

三对单,双引号的用法是定义的时候可以定义多行字符串

>>> a = """
... a
... b
... c
... d
... """
>>> print (a)

a
b
c
d

一对单,双引号也可以也可以定义多行字符串,但是要多麻烦有多麻烦

>>> b = "a\n"\
... "b\n"\
... "c\n"\
... "d\n"
>>> print(b)
a
b
c
d

当然三对单,双引号也是可以使用在一行定义一行的字符串

那么单引号和双引号有什么用.比如要输入Let's me think

>>> str = 'Let\'s me think'
>>> str
"Let's me think"

如果使用单引号,那么就要使用转译符

>>> str = "Let's me think"
>>> str
"Let's me think"

双引号就不需要

同样的如果字符串里面含有双引号比如:She said, "Hurry up". 

>>> str = 'She said, "Hurry up".'
>>> str
'She said, "Hurry up".'

使用单引号就不需要转译

>>> str = "She said, \"Hurry up\"."
>>> str
'She said, "Hurry up".'

使用双引号就需要转译

 

posted @ 2018-01-29 18:32  Dylan摆渡  阅读(7844)  评论(0编辑  收藏  举报