__str__方法:總結
成都創(chuàng)新互聯從2013年創(chuàng)立,是專業(yè)互聯網技術服務公司,擁有項目網站建設、成都網站設計網站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元肅南裕固族自治做網站,已為上家服務,為肅南裕固族自治各地企業(yè)和個人服務,聯系電話:13518219792
在python中方法名如果是__xxxx__()的,那么就有特殊的功能,因此叫做“魔法”方法,當使用print輸出對象的時候,只要自己定義了__str__(self)方法,那么就會打印從在這個方法中return的數據
例子1:如:
class Car:
def __init__(self, newWheelNum, newColor):
? ??self.wheelNum = newWheelNum
? ? 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))
#創(chuàng)建一個對象
tom = Cat("湯姆", 40)
lanmao = Cat("藍貓", 10)
print(tom)
print(lanmao)
運行結果:
湯姆的年齡是:40
藍貓的年齡是:10
str函數是Python的內置函數,它將參數轉換成字符串類型,其語法格式為str(object),返回object的字符串形式。
是將一個對象轉成字符串顯示,注意只是顯示用,有些對象轉成字符串沒有直接的意思。
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。
是將一個對象轉成字符串顯示,注意只是顯示用,有些對象轉成字符串沒有直接的意思。
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可以表示字符串類,也可以是將變量強制轉換為字符串的函數,寫作str()。str函數是Python內置函數的一種,可以直接使用,無需調用。 擴展資料
python中srt的全稱是SubRip Text,srt文件打開方式srt文件可以使用系統(tǒng)自帶的.文本處理器來打開,比如notepad.exe,write.exe,word等文件處理軟件。在Python中,str 表示字符串類 ,也可以是將變量強制轉換為字符串的函數,寫作str()。
Python中的str可以表示字符串類,也可以是將變量強制轉換為字符串的函數,寫作str()。str函數是Python內置函數的一種,可以直接使用,無需調用。
Python由荷蘭數學和計算機科學研究學會的Guido van Rossum于1990年代初設計,作為一門叫做ABC語言的替代品。Python提供了高效的高級數據結構,還能簡單有效地面向對象編程。
Python語法和動態(tài)類型,以及解釋型語言的本質,使它成為多數平臺上寫腳本和快速開發(fā)應用的編程語言,隨著版本的不斷更新和語言新功能的添加,逐漸被用于獨立的、大型項目的開發(fā)。
Python解釋器易于擴展,可以使用C或C++(或者其他可以通過C調用的語言)擴展新的功能和數據類型。Python也可用于可定制化軟件中的擴展程序語言。Python豐富的標準庫,提供了適用于各個主要系統(tǒng)平臺的源碼或機器碼。
標題名稱:strpython函數 c語言strpy
URL網址:http://www.chinadenli.net/article8/docceip.html
成都網站建設公司_創(chuàng)新互聯,為您提供做網站、網站建設、建站公司、響應式網站、商城網站、ChatGPT
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯