您的位置:首頁技術文章
文章詳情頁

Python %r和%s區別代碼實例解析

瀏覽:2日期:2022-07-31 09:59:04

%r用rper()方法處理對象

%s用str()方法處理對象

相同結果

有些情況下,兩者處理的結果是一樣的,比如說處理int型對象。

例:

print(’I am %s years old.’ % 22)print(’I am %r years old.’ % 22)

返回結果:

I am 22 years old.I am 22 years old.

不同結果

例:

x = 'There are %d types of people.' % 10print(’I said: %r’ %x)print(’I said: %s’ %x)

返回結果

I said: ’There are 10 types of people.’ # 通過%r 保留了原有所有屬性I said: There are 10 types of people.

例:

import datetimeriqi = datetime.date.today()print(riqi)print('%s' %riqi)print('%r' %riqi)

返回結果

2020-04-022020-04-02datetime.date(2020, 4, 2)

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
国产综合久久一区二区三区