1- 对象的方法:
这个对象类型在标准库里面就有的方法
2- 对象的方法调用
对象.方法
3- 字符串---str
1-count 计算字符串中包含的多少个指定的子字符串
str1 = 'abcaaa' ----str1.count('a') - 结果 4
2-endswith 检查字符串是否以指定的字符串结尾 --返回值 bool
3-startswith 检查字符串是否以指定的字符串开头 --返回值 bool
4-find 返回指定的子字符串在字符串中出现的位置
1- 只返回查找第一个出现的位置
2- str1.find('a',3) 指定开始查找下标位置
3- 如果要查找的内容,不在该对象里面,那么该方法返回 -1
5-isalpha 检查字符串中是否都是字母 ---返回值 bool
6-isdigit检查字符串中是否都是数字 ---返回值 bool
7-str.join将 sequence类型的参数的元素字符串合并(连接)到一个字符串,string 作为分隔符
alist = ['i','like', 'football']
print('*'.join(alist))
8-split将字符串分割为几个子字符串。参数为分隔符
str1 = 'abc,def,hijk'
print(str1.split(','))
1- 返回类型是list--列表
2- 那个切点还有吗? 切点会被切掉
9-lower 将字符串里面如果有大写字母的全部转为小写字母
10-upper 将字符串里面如果有小写字母的全部转为大写字母
11-replace 替换字符串里面指定的子字符串
str1 = 'abcaa'
print(str1.replace('a','x'))
注意点: 替换全部
12-strip 将字符串前置空格和后置空格删除 不能去中间空格
13-lstrip将字符串前置空格删除
14-rstrip将字符串后置空格删除
4- 列表
1-append,给列表添加一个元素 在列表尾部
2-insert,给列表指定位置插入一个元素
alist.insert(需要插入的位置的下标,插入的值)
3-列表删除元素
1- del alist[下标]
2- alist.pop(下标) 该方法有返回值 是被删元素
3- alist.remove(元素) ----效率最低
4- alist.clear--清空列表
5- reverse,将列表里面元素倒序排列
1- 字符串:
1- count--计算元素出现的次数
2- endswith 检查字符串是否以指定的字符串结尾 -bool
3- startswith 检查字符串是否以指定的字符串开头 -bool
4- find 返回指定的子字符串在字符串中出现的位置--如果有:下标;没有:-1
5- isdigit检查字符串中是否都是数字--bool
6- split将字符串分割为几个子字符串。参数为分隔符- 返回值- 列表
1- 切点将会被切掉
7- replace 替换字符串里面指定的子字符串--全部替换
2- 列表:
1- append--尾部
2- insert - 任意位置 insert(插入的位置-下标,值)
3- 删除元素
1- del alist[下标]
2- 删的值=alist.pop(下标)--有返回值
3- alist.remove(值)---无返回值
str1 = 'my*name*is-tom'
print(str1.split('*')[-1].split('-')[1])
print(str1.replace('-','*').split('*')[-1])
str2 = '2018-12-14--info:---driver is error!!!'
print(str1.split('my*name'))#列表
print(str1.replace('*','-',1))#-- 1 数量
print(str1)
def telSlect(inTel):
if len(inTel) == 11:
if inTel.isdigit():
if inTel.startswith('187') or inTel.startswith('139'):
print('移动用户!')
elif inTel.startswith('176') or inTel.startswith('132'):
print('联通用户!')
elif inTel.startswith('177') or inTel.startswith('153'):
print('电信用户!')
else:
print('没有该号段!')
else:
print('手机号存在非法字符!')
else:
print('您输入的手机号位数有误!')