(相关资料图)
(一)、作用
1. 单引号:单引号内部为一串字符(str)。
2. 双引号:双引号内部为一串字符,双引号内的字符串可以出现单引号(相当于双引号优先级更高),但不能嵌套双引号。
3. 三引号:用于换行输出,且缩进会被打印出来。(三引号还可以用来进行块注释)。
(二)、共同点
- python中变量赋值字符串均可使用三者符号来表示。
- 三者都可以直接引用字符串是等价的。
(三)、不同点
- 双引号中可以包含单引号,避免使用单引号包含字符串频繁使用转义符。
- 三引号中可以包含换行符、制表符以及其他特殊字符(即在读取转义符的基础上完全复现引号内输入的全部内容,避免频繁使用换行符/n。
- 可以包含#注释,自身可以作为多行注释标志符使用。
- 三引号也可以表示字符串,而且能够保留字符串的格式,里面也可以包含单引号和双引号,但是单引号和双引号不能包含三引号。
- 如果想要单引号嵌套单引号,双引号嵌套双引号,可以用转义字符。
- python中单引号和双引号还可以嵌套,但是必须注意可以相互嵌套,但是单引号不可以直接嵌套单引号,双引号不可以直接嵌套双引号。
(四)、演示示例
"""一、单引号"""##single_str = "Single quotation mark"## 单引号嵌套双引号single_str2 = "Tom asked me "Where are you from?""## 单引号嵌套单引号,需要进行转义single_str3 = "I\"m a student"print(single_str)print(single_str2)print(single_str3)print("-"*100)"""二、双引号"""double_str = "Double quotation mark"## 双引号嵌套单引号double_str2 = "I"m a student"## 双引号嵌套双引号,需要进行转义double_str3 = "Tom asked me \"Where are you from?\""print(double_str)print(double_str2)print(double_str3)print("-"*100)"""三、三引号"""## 三个单引号three_str = """Three quotation mark"""## 三个双引号three_str2 = """Captain,My Capation!"""## 三个单引号并换行three_str3 = """ SELECT years, country, product, SUM(sales) AS total_sales FROM sales GROUP BY years, country, product """## 三个双引号并换行three_str4 = """ SELECT IF(GROUPING(years), "All years", years) AS years, IF(GROUPING(country), "All countries", country) AS country, IF(GROUPING(product), "All products", product) AS product, SUM(sales) AS Total_Sales FROM sales GROUP BY years, country, product WITH ROLLUP; """print(three_str)print(three_str2)print(three_str3)print(three_str4)
结果输出:
关键词: