1. bool()
a = bool(None)
b = bool()
c = bool({})
d = bool([])
e = bool(0)
print(a,b,c,d,e)
_____________
False False False False False
2. 字母數字轉化
a = 123
b = str(a)
print(b,type(b))
123 <class 'str'>
a = "123" b = int(a) print(b,type(b)) ———————— 123 <class 'int'>
3. range()
python 2 中,一次性立即創建;
Python 3中,for循環是一個一個創建
range(0, 100, 1) 正序
range(0, 100, -1) 倒序
4. 分隔和轉換
a = input("5+9輸入:")
a1,a2 = a.split('+')
b1 = int(a1)
b2 = int(a2)
print(a1,type(a1),'\n',a2,type(a2),'\n',b1,type(b1),'\n',b2,type(b2),'\n',b1+b2)
——————————————————————————————————————
5+9輸入:5+9
5 <class 'str'>
9 <class 'str'>
5 <class 'int'>
9 <class 'int'>
14
5. replace
a = 'abhjdlksa'
b= a.replace('s','&&&&&')
print(a,'\n',b)
_______________
abhjdlksa
abhjdlk&&&&&a
6. 列表字符串轉換
li = ['12b',123,'hjdka',897,78,2233,'o83ijfk']
s = ' '
for i in li:
s = s + str(i)
print(s)
——————————————
12b123hjdka897782233o83ijfk
浙公網安備 33010602011771號