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),' ',a2,type(a2),' ',b1,type(b1),' ',b2,type(b2),' ',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,' ',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