python3.x - Python3,关于str.format的语法
本文介绍了python3.x - Python3,关于str.format的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
描述问题
format的用法,代码中的!r
!s
分别是format的哪部分用法
我查阅文档, 未能找到这部分的说明(https://docs.python.org/3/lib...
使用搜索引擎,也不知如何组织关键词
上下文环境
Python3
重现
拷贝代码
运行之
相关代码
class Pair:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return 'Pair({0.x!r}, {0.y!r})'.format(self)
def __str__(self):
return '({0.x!s}, {0.y!s})'.format(self)
>>> p = Pair(3, 4)
>>> p
Pair(3, 4) # __repr__() output
>>> print(p)
(3, 4) # __str__() output
解决方案
!r
就是 r
epr!s
就是 s
tr!a
就是 a
scii
Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().
Some examples:
"Harold's a clever {0
!s
}" # Callsstr()
on the argument first
"Bring out the holy {name!r
}" # Callsrepr()
on the argument first
"More {!a
}" # Callsascii()
on the argument first
这篇关于python3.x - Python3,关于str.format的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文