python基础之单引号、双引号和三引号

在以前C或java语言中,我们都习惯使用双引号表示字符串,但在学习python时出现了三种引号。
一:单引号和双引号
 >>> "hello world"    ..........使用双引号
'hello world'
>>> 'hello world'     ...........使用单引号
'hello world'
可见,单引号和双引号,都可以表示字符串的起和终,引起在这种场景中使用是没有区别的。
>>> "this is my "world"" .............双引号中有双引号
SyntaxError: invalid syntax
这个时候编译器就不知道哪个引号是起或者终。
>>> "this is my 'world'"   ...........双引号中有单引号
"this is my 'world'"
>>> 'this is my "world"'  ...............单引号中有双引号
'this is my "world"'
所以在表示一个字符串时,串中还有引号,需要单、双引号配合使用,形成引号一对一。

>>> 'Let\'s go!'              使用反斜线也可以实现。
"Let's go!"
>>> "Let's go!"
"Let's go!"

二:双引号和三引号的区别
双引号所表示的字符串通常要写成一行。但如果要写成多行
>>> "hello,\                ............使用(\)连接符,这个和C语言中define换行相同
world"
'hello,world'
如果使用三引号,可以直接写成:
>>> """hello,
world"""
'hello,\nworld'
>>> 

可能是我C语言用的比较多,所以还是双引号和连接符配合使用。

猜你喜欢

转载自blog.csdn.net/Calm_027/article/details/69305558