What is the difference between python single quotes, double quotes, and triple quotes?

青灯夜游
Release: 2023-01-03 09:26:15
Original
22819 people have browsed it

区别:单引号和双引号是等效的,如果要换行,那么需要使用符号“\”;三引号则可以直接换行,并且可以包含注释。单引号里不能加单引号,但可加“\”或者是双引号进行转义输出。双引号里面不能再加双引号,但是可以加“\”或者是单引号进行转义输出。

What is the difference between python single quotes, double quotes, and triple quotes?

本教程操作环境:windows7系统、Dell G3电脑、Python3。

在python中,单引号、双引号功能一样,都可以表示字符串,也都可以起到转义的功能。

例如:

print('-\t-\\-\'-%-/-\n')
print("-\t-\\-\'-%-/-\n")
Copy after login

他们的显示结果都是一样的:都显示转义后的字符:

-    -\-'-%-/-
Copy after login

其实在转义这一点上,三个引号的功能也是一样的,例如:

print('''-\t-\\-\'-%-/-\n''')
print("""-\t-\\-\'-%-/-\n""")
Copy after login

同样都会显示上面的结果。

但是,三引号的独特之处在这里:可以显示多行
例如:

# 三单引号(''')
print('''i
love
you''')

# 三双引号(""")
print(""" I can print ''' """)
print("""i
love
you""")
Copy after login

上面两端代码的打印效果是一样的,都显示:

i
love
you
Copy after login

所以总结一下就是:

单引号和双引号是等效的,如果要换行,那么需要使用符号(\)。单引号里面不能再加单引号,但是可以加 \ 或者是双引号进行转义输出。双引号里面不能再加双引号,但是可以加 \ 或者是单引号进行转义输出。

三引号括起来的字符串可以直接进行换行,并且可以包含注释。

相关推荐:Python3视频教程

不转义字符串

上述表示都是转义的,那么如何实现不转义的表示字符呢。

在字符串前加一个r即可。

# 不转义
print(r'\n')

结果:
\n
Copy after login

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of What is the difference between python single quotes, double quotes, and triple quotes?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!