python打印字符串的方法
python打印字符串的方法
1、说明
__str__主要应用于print函数以及字符串函数str的转换操作
__repr__应用于所有输出操作,如果有print以及str操作并定义__str__,则会以__str__为准
__repr__与__str__均未定义的时候,默认打印的是输出对象地址信息
2、实例
#str.pyclassDisplayClass:
"""
__repr__isusedeverywhere,exceptbyprintandstrwhena__str__isdefined.
__str__tosupportprintandstrexclusively
"""
def__repr__(self):
return"display__repr__class"
def__str__(self):
return"display__str__class"#使用命令行的形式打印输出2.x&3.x输出效果一致,以2.x作为截图>>>d=DisplayClass()>>>d#调用repr>>>print(d)#调用str>>>print(repr(d))#调用repr>>>print(str(d))#调用str
以上就是python打印字符串的方法,希望对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python函数标注是什么
python函数标注是什么在参数的使用方法中,还有一种解包的情况是需要我们掌握的。比如,将列表或者字典的值转换为函数的参数,就需要用到参数解...详情>>
2023-11-14 14:13:35
python正负索引的使用
python正负索引的使用本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、说明使用正索引(从左至右)、负索引(从右至左)访问元素,时...详情>>
2023-11-14 13:45:04
python垃圾回收的机制过程
python垃圾回收的机制过程本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。1、步骤(1)收集所有容器对象(list,dict,tuple,customClas...详情>>
2023-11-14 13:05:15
pythontry语句的执行原理
pythontry语句的执行原理1、原理分析(1)执行try子句(try和except关键字之间的多行语句)。(2)如果没有异常发生,则跳过except子句并完成try语句...详情>>
2023-11-14 09:37:35热门推荐
技术干货






