新聞中心
1、說明

專注于為中小企業(yè)提供網(wǎng)站設(shè)計(jì)制作、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)武進(jìn)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上千企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
__str__主要應(yīng)用于print函數(shù)以及字符串函數(shù)str的轉(zhuǎn)換操作
__repr__應(yīng)用于所有輸出操作,如果有print以及str操作并定義__str__,則會(huì)以__str__為準(zhǔn)
__repr__與 __str__均未定義的時(shí)候,默認(rèn)打印的是輸出對(duì)象地址信息
2、實(shí)例
# str.pyclass DisplayClass: """ __repr__ is used everywhere, except by print and str when a __str__ is defined. __str__ to support print and str exclusively """ def __repr__(self): return "display __repr__ class" def __str__(self): return "display __str__ class"# 使用命令行的形式打印輸出 2.x & 3.x 輸出效果一致,以2.x作為截圖>>> d = DisplayClass()>>> d # 調(diào)用repr>>> print(d) # 調(diào)用str>>> print(repr(d)) # 調(diào)用repr>>> print(str(d)) # 調(diào)用str
以上就是python打印字符串的方法,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)Python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
名稱欄目:創(chuàng)新互聯(lián)Python教程:python打印字符串的方法
分享網(wǎng)址:http://www.dlmjj.cn/article/cdjcood.html


咨詢
建站咨詢
