Python中引号的使用
Python中的引号有单引号
'**'
、双引号"**"
、三引号"""**"""
。
本文中所指的Python开发环境是Python2。
单引号'**'
和双引号"**"
在Python中的单引号'**'
、双引号"**"
基本没有任何区别,可以根据个人的使用习惯进行决定使用那一个。不过在使用的过程中仍有一些注意事项。
# 使用的是单引号,错误的使用方法
print 'This's the test sentence'
# 使用单引号,正确的使用方法
print 'This\'s the test sentence'
# 使用的是双引号
print "This's the test sentence"
在上面的例子中,使用单引号出现错误。因为英语的使用习惯,在句子中常常使用到'
这就导致错误的出现原因。
建议就是,在使用长句子的时候,尽量使用双引号,也就避免了这样的问题的出现。
双引号"**"
和三引号"""**"""
当在需要输入一个较长的句子的时候,由于一行能输入的内容有限需要进行转行,在转行的过程中就需要是用到\
。
# 一个过长的句子
# "A great man once said it is necessary to do as much as possible,and the more you apply it in real situations,the more natural it will become."
# 多行进行输入
print "A great man once said it is necessary\
to do as much as possible,and the more you apply it\
in real situations,the more natural it will become."
# 使用三引号
print"""
A great man once said it is necessary
to do as much as possible,and the more you apply
it in real situations,the more natural it will become.
"""
在上面的例子中,由于句子较长使用双引号"**"
的时候在进行转行的过程中需要使用的符号\
可以让多行的内容连接在一起。使用"""**"""
也就直接可以进行转行,无需其他的注意事项。
不过在使用"""**"""
的时候,每行的末端相当于默认的输入了一个转行的转义符\n
。