⑴ python str函數怎麼用
是將一個對象轉成字元串顯示,注意只是顯示用,有些對象轉成字元串沒有直接的意思。
⑵ python字元串怎麼和整數連接
1、在python中完成字元串和數字的拼接,可以使用內置函數str()。
⑶ python中「str」是什麼意思
字元串的意思。
⑷ python中的__str__函數作用
__str__方法:總結
在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 introce(self):
print("%s的年齡是:%d"%(self.name, self.age))
#創建一個對象
tom = Cat("湯姆", 40)
lanmao = Cat("藍貓", 10)
print(tom)
print(lanmao)
運行結果:
湯姆的年齡是:40
藍貓的年齡是:10
⑸ Python字元編碼使用什麼碼
在python 2中默認編碼是ASCII,而在python 3中默認編碼是unicode。
⑹ python3的print怎麼輸出utf8
python3內部都是unicode表示print(str)的結果重定向以後,依賴於平台的編碼設置比如[*]str="中文"print(str)python33python.exe test.py out.txt 打開out.txt是gb2312編碼的print(str)有沒有辦法象寫文件那樣可以指定編碼比如f=open('out.txt','w',encoding='utf-8')
⑺ python中str是什麼意思
將某一個類型強制轉換為字元串型。
如,a = 1,a的類型就是數值型,a = str(a),a就是字元串型了