欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python中的str函數(shù),str函數(shù) python

python中str是什么意思?

str函數(shù)是Python的內(nèi)置函數(shù),它將參數(shù)轉(zhuǎn)換成字符串類型,其語(yǔ)法格式為str(object),返回object的字符串形式。

成都創(chuàng)新互聯(lián)是一家專業(yè)提供拜泉企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、做網(wǎng)站成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為拜泉眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

python中的__str__函數(shù)作用

__str__方法:總結(jié)

在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做“魔法”方法,當(dāng)使用print輸出對(duì)象的時(shí)候,只要自己定義了__str__(self)方法,那么就會(huì)打印從在這個(gè)方法中return的數(shù)據(jù)

例子1:如:

class Car:

def __init__(self, newWheelNum, newColor):

? ??self.wheelNum = newWheelNum

? ? self.color = newColor

def __str__(self):

? ??msg = "嘿。。。我的顏色是" + self.color + "我有" + int(self.wheelNum) + "個(gè)輪胎..."

? ??return msg

def move(self):

? ??print('車在跑,目標(biāo):夏威夷')

BMW = Car(4, "白色")

print(BMW)

例子2:如:

class Cat:

"""定義了一個(gè)Cat類"""

#初始化對(duì)象

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("貓?jiān)诔贼~....")

def drink(self):

? ? print("貓正在喝kele.....")

def introduce(self):

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

#創(chuàng)建一個(gè)對(duì)象

tom = Cat("湯姆", 40)

lanmao = Cat("藍(lán)貓", 10)

print(tom)

print(lanmao)

運(yùn)行結(jié)果:

湯姆的年齡是:40

藍(lán)貓的年齡是:10

python str函數(shù)怎么用

是將一個(gè)對(duì)象轉(zhuǎn)成字符串顯示,注意只是顯示用,有些對(duì)象轉(zhuǎn)成字符串沒(méi)有直接的意思。

str():將變量轉(zhuǎn)化為字符串類型

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。

網(wǎng)站標(biāo)題:python中的str函數(shù),str函數(shù) python
URL地址:http://chinadenli.net/article7/dsgecij.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈移動(dòng)網(wǎng)站建設(shè)品牌網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計(jì)面包屑導(dǎo)航小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)