首先所有的引号要成对出现,就像穿鞋要成双
一、单引号和双引号
1.单引号和双引号单独出现时,二者输出结果没有区别
>>>str1="the good wife"
>>>str2='the good wife'
>>>print(str1)
the good wife
>>>print(str2)
the good wife
2.当单引号和双引号同时出现时,最外层引号包含的内容则为字符串
# 输出字符串中的单引号
>>>str3="he is a 'cute' boy"
>>>print(str3)
he is a 'cute' boy
# 输出字符串中的双引号
>>>str4='he is a "cute" boy'
>>>print(str4)
he is a "cute" boy
# 不可出现两对相同的引号,下面两个程序将会报错
>>>str5='he is a 'cute' boy'
>>>str6="he is a "cute" boy"
当然,也有其他的办法使用相同的引号呈现同样的效果,那就是加入转义符\
# 在引号前加入转义符\
>>>str5 = 'he is a \'cute\' boy.'
>>>print(str5)
he is a 'cute' boy.
二、三引号的用法
通常情况下,三单引号和三双引号是没有区别的
当我们想打多行代码时,会在代码后面加入\,但是输出的结果不是多行的
# 代码后加入\,实现多行代码编写
>>>str6 = "There is no solace above or below.Only us... \
Small, solitary,battling one another.\
I pray to myself, for myself."
>>>print(str6)
There is no solace above or below.Only us... Small, solitary,battling one another. I pray to myself, for myself.
此时,我们可以使用三引号
>>>str7 = """
There is no solace above or below.Only us...
Small, solitary,battling one another.
I pray to myself, for myself."""
>>>print(str7)
There is no solace above or below.Only us...
Small, solitary,battling one another.
I pray to myself, for myself.