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

穆沙希德·汗(Mushahid Khan)

是什么区别printobjectrepr()为什么要以不同的格式打印?

参见output difference

>>> x="This is New era"
>>> print x             # print in double quote when with print()
This is New era

>>> x                   #  x display in single quote
'This is New era'

>>> x.__repr__()        # repr() already contain string
"'This is New era'"

>>> x.__str__()         # str() print only in single quote ''
'This is New era'
tobias_k

'之间没有语义差异"'如果字符串包含",反之亦然,可以使用,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 printstrand reprprint打印给定的字符串而没有其他引号,而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 echo
some string
>>> str("some string")   # create string, echo result
'some string'
>>> repr("some string")  # create repr string, echo result
"'some string'"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用PHP在单引号和双引号之间查找内容

在Bash中在双引号和单引号中使用反引号的区别

使用单引号和双引号时,JSON密钥有什么区别吗?

trap命令使用的单引号和双引号有什么区别?

在python中使用urllib时,双引号和单引号字符串之间的行为不同

执行命令但已使用单引号和双引号

在wpdb select中使用双引号和单引号

如何在JavaScript中使用单引号和双引号

Scala:使用双引号(“”)和单引号(“)拆分

单引号和双引号使用data.replace的问题

包装awk代码时使用双引号和单引号

在python字典中使用单引号和双引号有什么区别(如果有)?

何时在MySQL中使用单引号,双引号和反引号

如果已经使用了单引号和双引号,该如何用引号引起来?

使用Awk删除引号(双引号或单引号)

Flutter / Dart中单引号和双引号之间的区别

JavaScript中单引号和双引号之间的区别

Bash中单引号和双引号之间的区别

单引号和双引号的文字之间的Yacc /野牛区别

在Apache Spark的Scala API中,使用单引号和$“”表示法之间有区别吗?

使用python 2.7使用单引号,双引号和Unicode解析JSON

如何在sed中使用单引号和双引号同时使用变量

postgres 数据库查询。如何使用单引号和双引号以及如何使连接工作

仅使用bash /标准Linux命令去除字符串中的单引号和双引号

在 bash 脚本中使用双引号和单引号来调用程序

使用双引号和单引号将字符串从EF保存到SQL

如何使用sed命令同时匹配双引号和单引号?

如何使用双引号和单引号格式化python字符串

如何在脚本化的ssh命令中使用单引号和双引号