【python入门笔记】python中单引号、双引号、三引号的用法

本文介绍了Python中单引号、双引号和三引号的用法。单引号与双引号用于创建字符串,成对出现,输出结果相同。在包含引号的字符串中,可以使用转义符\。三引号常用于多行字符串,方便进行多行代码的注释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先所有的引号要成对出现,就像穿鞋要成双

一、单引号和双引号

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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值