What is the difference between !r and %r in Python?

我只是一个虾纸丫 提交于 2020-01-11 15:20:04

问题


As the title states, what is the difference between these two flags? It seems they both convert the value to a string using repr()? Also, in this line of code:

"{0!r:20}".format("Hello")  

What does the 0 in front of the !r do?


回答1:


%r is not a valid placeholder in the str.format() formatting operations; it only works in old-style % string formatting. It indeed converts the object to a representation through the repr() function.

In str.format(), !r is the equivalent, but this also means that you can now use all the format codes for a string. Normally str.format() will call the object.__format__() method on the object itself, but by using !r, repr(object).__format__() is used instead.

There are also the !s and (in Python 3) !a converters; these apply the str() and ascii() functions first.

The 0 in front indicates what argument to the str.format() method will be used to fill that slot; positional argument 0 is "Hello" in your case. You could use named arguments too, and pass in objects as keyword arguments:

"{greeting!r:20}".format(greeting="Hello")  

Unless you are using Python 2.6, you can omit this as slots without indices or names are automatically numbered; the first {} is 0, the second {} takes the second argument at index 1, etc.



来源:https://stackoverflow.com/questions/33097143/what-is-the-difference-between-r-and-r-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!