內建物件方法

2022-07-12 05:00:13 字數 794 閱讀 3772

常用內建物件方法

name = tom

age = 20

# 方法1.使用佔位符格式化

print("名字:%s,年齡:%d"%(name,age))

# 方法2:格式化

print(f"名字:,年齡:")

# 方法3:格式化

print("名字 :年齡".format(name,age))

# 賦值

s = '

hello's[

0] --->第乙個

s[-1] --->最後乙個

# 字串不可變

s = '

helloworld's[

5] ='w'

#報錯 str not support item assignment

# 遍歷

str01='

hello

'for i in

hello:

print(i)

# 編列索引和值

for i,v in

enumerate(str01):

print(f

'第,個值:')

# 生成a-z print(chr(random.choice(range(97,123

內建物件方法

1 array方法 原型物件上的方法,可直接通過例項呼叫 foreach 遍歷陣列方法 沒返回值 也就是undefined map 原始陣列呼叫函式處理後的值,返回新陣列 every 判斷陣列中每個元素是否滿足,返回布林值 some 判斷陣列中所有元素是否滿足,返回布林值 include 判斷陣列中...

物件導向內建方法

判斷obj是否是cls類的物件 例 li 1,2,3,4 def run list li for i in li if isinstance i,list run list i else print i run list li 判斷sub是否是super的派生類通過字串的形式操作物件的相關屬性 1 ...

內建物件相關方法

內建物件相關方法 常用內建物件的方法 字串string name tom age 20 方法1.使用佔位符格式化 print 名字 s,年齡 d name,age 方法2 格式化 print f 名字 年齡 方法3 格式化 print 名字 年齡 format name,age 轉義 t 製表符等於...