Python中引号的使用

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值