1. 首页 > 电脑手机 >

str在python中是什么意思 python必背入门代码

python中的__str__函数作用

__str__方法:总结

在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做“魔法”方法,当使用print输出对象的时候,只要自己定义了__str__(self)方法,那么就会打印从在这个方法中return的数据

例子1:如:

class Car:

def __init__(self, newWheelNum, newColor):

self.wheelNum = newWheelNum

str在python中是什么意思 python必背入门代码str在python中是什么意思 python必背入门代码


self.color = newColor

def __str__(self):

msg = "嘿。。。我的颜色是" + self.color + "我有" + int(self.wheelNum) + "个轮胎..."

return msg

def move(self):

print('车在跑,目标:夏威夷')

BMW = Car(4, "白色")

print(BMW)

例子2:如:

class Cat:

"""定义了一个Cat类"""

#初始化对象

def __init__(self, new_name, new_age):

self.name = new_name

self.age = new_age

def __str__(self):

return "%s的年龄是:%d"%(self.name, self.age)

#方法

def eat(self):

print("猫在吃鱼....")

def drink(self):

print("猫正在喝kele.....")

def introduce(self):

print("%s的年龄是:%d"%(self.name, self.age))

#创建一个对象

tom = Cat("汤姆", 40)

lanmao = Cat("蓝猫", 10)

print(tom)

print(lanmao)

运行结果:

汤姆的年龄是:40

蓝猫的年龄是:10

Python中repr()函数和str()的区别

repr()和str,一个最简单的区别:repr是函数,str跟int一样是一种对象类型。

repr ( object )

返回对象的可打印形式字符串。对于很多类型而言,本函数试图返回的字符串,会与将对象传给 eval() 所生成的结果相同;不然,结果就是一个尖括号包裹的字符串,包含了对象类型名称及其附加信息,附加信息通常包括对象的名称和内存地址。通过定义 __repr__() 方法,类可以控制本函数将为实例返回的内容。

class str ( object='' ) class str ( object=b'' , encoding='utf-8' , errors='strict' )

str在python中是什么意思 python必背入门代码str在python中是什么意思 python必背入门代码


返回 object 的 字符串 版本。 如果未提供 object 则返回空字符串。 在其他情况下 str() 的行为取决于 encoding 或 errors 是否有给出,具体见下。

如果 encoding 或 errors 均未给出,str(object) 返回 object.__str__(),这是 object 的“非正式”或格式良好的字符串表示。 对于字符串对象,这是该字符串本身。 如果 object 没有 __str__() 方法,则 str() 将回退为返回 repr(object)。

如果 encoding 或 errors 至少给出其中之一,则 object 应该是一个 bytes-like object (例如 bytes 或 bytearray)。 在此情况下,如果 object 是一个 bytes (或 bytearray) 对象,则 str(bytes, encoding, errors) 等价于 bytes.decode(encoding, errors)。 否则的话,会在调用 bytes.decode() 之前获取缓冲区对象下层的 bytes 对象。 请参阅 二进制序列类型 --- bytes, bytearray, memoryview 与 缓冲协议 了解有关缓冲区对象的信息。

将一个 bytes 对象传入 str() 而不给出 encoding 或 errors 参数的操作属于第一种情况, 将返回非正式的字符串表示(另请参阅 Python 的 -b 命令行选项)。 例如:

什么时候应该使用str(),什么时候应该使用repr()?

在为最终用户创建输出时,几乎总是使用str。repr()主要用于调试和 探索 。例如,如果您怀疑字符串中有非打印字符,或者浮点数有一个小的舍入错误,则repr()将显示给您;str可能不会。repr()在生成要粘贴到源代码中的文字时也很有用。

python str是什么编码

str 和 unicode

str和unicode都是basestring的子类

所以有判断是否是字符串的方法

def is_str(s): return isinstance(s, basestring)

str和unicode 转换

decode 文档

encode 文档

str -> decode(‘the_coding_of_str‘) -> unicode unicode -> encode(‘the_coding_you_want‘) -> str

区别

str是字节串,由unicode经过编码(encode)后的字节组成的

str在python中是什么意思 python必背入门代码str在python中是什么意思 python必背入门代码


声明方式

s = ‘中文‘ s = u‘中文‘.encode(‘utf-8‘) >>> type(‘中文‘)

求长度(返回字节数)

>>> u‘中文‘.encode(‘utf-8‘) ‘\xe4\xb8\xad\xe6\x96\x87‘ >>> len(u‘中文‘.encode(‘utf-8‘)) 6

unicode才是真正意义上的字符串,由字符组成

声明方式

s = u‘中文‘ s = ‘中文‘.decode(‘utf-8‘) s = unicode(‘中文‘, ‘utf-8‘) >>> type(u‘中文‘)

求长度(返回字符数),在逻辑中真正想要用的

>>> u‘中文‘ u‘\u4e2d\u6587‘ >>> len(u‘中文‘) 2

str在python中用法

Python中的str可以表示字符串类,也可以是将变量强制转换为字符串的函数,写作str()。str函数是Python内置函数的一种,可以直接使用,无需调用。 扩展资料

python中srt的全称是SubRip Text,srt文件打开方式srt文件可以使用系统自带的.文本处理器来打开,比如notepad.exe,write.exe,word等文件处理软件。在Python中,str 表示字符串类 ,也可以是将变量强制转换为字符串的函数,写作str()。

python str函数怎么用

是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思。

str():将变量转化为字符串类型

a = 1

b = [1, 2, 3]

str_a = str(a)

print(a)

print(type(a))

str_b = str(b)

print(b)

print(type(b))

The str() function is meant to return representations of values which are fairly human-readable, while repr() is meant to generate representations which can be read by

the interpreter (or will force a SyntaxError if there is not equivalent syntax). For objects which don't have a particular representation for human consumption, str() will

return the same value as repr(). Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function. Strings and。

Python中的str,len是什么?什么时候要用到?还有python有什么用?java又有什么用

1、str是内置的函数,我们可以把其他类型的数(如,整数)通过str转化为字符串

2、len是用来返回容器中元素的个数

3、python可以用来做数据分析

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至836084111@qq.com 举报,一经查实,本站将立刻删除。

联系我们

工作日:9:30-18:30,节假日休息