在Python中,字符串是最罕用的数据类型。引号('或')可用于创立字符串。

一、打印字符串

1.__str__次要利用于print函数以及字符串函数str的转换操作

2.__repr__利用于所有输入操作,如果有print以及str操作并定义__str__,则会以__str__为准

3.__repr__与 __str__均未定义的时候,默认打印的是输入对象地址信息

二、实例

# 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                   # 调用repr>>> print(d)            # 调用str>>> print(repr(d))      # 调用repr>>> print(str(d))       # 调用str

字符串相干知识点,举荐拜访:字符串

以上就是python打印字符串的办法,心愿对大家有所帮忙。更多Python学习指路:python基础教程