单引号,双引号,三引号

1、单引号,双引号 用来表示字符串str时必须是成对出现的

>>> 'hello word'
'hello word'
>>> "hello word"
'hello word'
>>> "Let's go"
"Let's go"
>>> 'Let's go'
SyntaxError: invalid syntax
>>> 'Let"s go'
'Let"s go'

注:上面例子中的lets go 中的引号是字符串中的一部分,不是用来表示字符串的,所以可以单独显示

2、转义字符’\‘

>>> 'let\'s go'
"let's go"

3、’\‘反斜线也可以用来换行

>>> 'hello\
world'
'helloworld'

4、三引号一般主要用于换行

>>> 'hel
SyntaxError: EOL while scanning string literal
>>> "hell
SyntaxError: EOL while scanning string literal
>>> '''hello
word'''
'hello\nword'
>>> """
hello word
	"""
'\nhello word\n\t'

注:IDLE中换行后显示,会把回车的字符显示出来即 ’\n‘  ,  tab字符也显示出来 ’\t‘

5、IDLE中的显示与print打印显示不一样,print时会把\n,\t都显示出来

>>> '\nhello word\n'
'\nhello word\n'
>>> print('\nhello word')

hello word



猜你喜欢

转载自blog.csdn.net/weixin_41355124/article/details/80309803