栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用__repr __()了解双引号和单引号之间的区别

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用__repr __()了解双引号和单引号之间的区别

'
和之间没有语义差异
"
'
如果字符串包含
"
,反之亦然,则可以使用,Python将执行相同的操作。如果字符串包含两个字符串,则必须转义其中的一些(或使用三引号
"""
'''
)。(如果这两个
'
"
是可能的,Python和很多程序员似乎更喜欢
'
,虽然)。

>>> x = "string with ' quote">>> y = 'string with " quote'>>> z = "string with ' and " quote">>> x"string with ' quote">>> y'string with " quote'>>> z'string with ' and " quote'

about

print
str
and
repr
print
打印 给定的字符串而没有其他引号,而
str
将从给定的对象 创建
一个字符串(在这种情况下为字符串本身),并从该对象
repr
创建
一个“表示字符串”(即,包含一组字符串的字符串)引号)。简而言之,之间的区别
str
,并
repr
应该是
str
很容易理解 的用户
,并
repr
很容易理解 为Python


另外,如果您在交互式外壳程序中输入任何表达式,Python会自动回

repr
显结果。这可能有点令人困惑:在交互式外壳中,执行此操作
print(x)
时所
看到的
str(x)
;使用时
str(x)
,看到的是
repr(str(x))
,使用时
repr(x)
,看到的
repr(repr(x))
(因此双引号)。

>>> print("some string") # print string, no result to echosome string>>> str("some string")   # create string, echo result'some string'>>> repr("some string")  # create repr string, echo result"'some string'"


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669517.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号