%r或者{!r}在python中的意思
生活随笔
收集整理的這篇文章主要介紹了
%r或者{!r}在python中的意思
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這兩個都是python的轉譯字符, 類似于%r, %d,%f
有過c語言基礎的朋友應該會比較容易上手吧?
簡單的例子
上面的例子用的是format,跟直接%效果類似。
例子二
>>> a = '123' >>> b = 'hello, %r' % a >>> b "hello, '123'"這對一部分的對象還是很有用的。r直接反應對象本體。
比如說,看看下面的這個、
>>> b = 'hello, %r' % 123 >>> 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'"更多操作值得挖掘~
總結
以上是生活随笔為你收集整理的%r或者{!r}在python中的意思的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多进程爬虫(爬取小说)Python实现
- 下一篇: 创建文件夹(如果已经存在就清空)pyth