python '%r'或者'{!r}'的意思


轉載:https://blog.csdn.net/a19990412/article/details/80149112

 

 

這兩個都是python的轉譯字符, 類似於%r, %d,%f

 

>>> a = '123'
>>> b = 'hello, {!r}'.format(a)
>>> b
"hello, '123'"

上面的例子用的是format,跟直接%效果類似。




>>> a = '123'
>>> b = 'hello, %r' % a
>>> b
"hello, '123'"

這對一部分的對象還是很有用的。r直接反應對象本體。






>>> a = '123'
>>> b = 'hello, %r' % a
>>> b
"hello, '123'"

123的本體就是123。





>>> b = 'hello, !r' % '123'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not all arguments converted during string formatting
>>>
--------------------- 

!符號,這個只在fromat中有用,要注意方法。但是效果類似
>>> b = 'hello, %r' % '123'
>>> b
"hello, '123'"
>>> b = 'hello, {!r}'.format( '123')
>>> b
"hello, '123'"

 

 

更多操作有時間的話就去挖掘~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM