序列类型的内建函数

判断成员是否属于序列

aList = ['Mon', 'Tus', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
print('Mon' in aList)
aList = ['Mon', 'Tus', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
print('Mon'not in aList)

序列类型转换内建函数

  • list:字符串,元组转序列
str = 'Hello World'
a = list(str)
print(a)
  • tuple:字符串,列表转为元组
str = 'Hello World'
a = tuple(str)
print(a)
  • str
print(type(str(123)))
#<class 'str'>

其他序列类型的内建函数

  • len:长度
aList = ['Mon', 'Tus', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
len = len(aList)
print('序列的长度是',len)
  • sorted:排序
aList = [1,4,2,5,6,78,0,9]
a = sorted(aList)
print(a)
  • reversed:逆序排列
aList = [1,4,2,5,6,78,0,9]
a = list(reversed(aList))
#值得注意的地方
print(a)
  • sum:求和
aList = [1,4,2,5,6,78,0,9]
a = sum(aList)
print(a)
  • max:最大值
aList = [1,4,2,5,6,78,0,9]
a = max(aList)
print(a)
  • min:最小值
aList = [1,4,2,5,6,78,0,9]
a = min(aList)
print(a)
  • enumerate:元素加索引
aList = ['Mon', 'Tus', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
a = list(enumerate(aList))
print(a)
  • zip:拆分重组
a = 'hello'
b = 'world'
c = list(zip(a,b))
print(c)
#[('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]

字符串的方法

  • aStr.center():宽度居中
aStr = 'hello'
print(aStr.center(15))
  • count():判断字符或者子串的个数
aStr = 'hello'
print(aStr.count('l'))
  • find():返回
aStr = 'hello'
print(aStr.find('lo'))
  • replace:替换某个字符串
= 'hello'
print(aStr.replace('h', "H"))
  • split方法,默认按空格分开这个字符串,返回一个序列
aStr = 'he llo wo rld'
print(aStr.split())
结果:['he', 'llo', 'wo', 'rld']
  • strip():去掉字符串两端的空格
aStr = ' hello world '
print(aStr.strip())