Python:对象方法

'''''
知识点汇总:
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',n))
        注意点: 替换全部
    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(值)---无返回值
'''
# 1.计算字符串中子字符串出现的次数
info = 'abcdefaa'
print(info.count('a'))
print(info.count('x'))
if info.count('x') > 0:  # 跟in用法效果一样,也可以用于连续字符串
    print('元素存在')
else:
    print('元素不存在')

# 2.endswith-以指定的字符串结尾
if info.endswith('a'): #返回是bool
    print('幸运用户!')
# 3.startswith-以指定的字符串开头
if info.startswith('a'):#返回是bool
    print('幸运用户!')

# 4.find-子字符串在字符串中出现的位置
print(info.find('d'))
print(info.find('a',info.find('a')+1)) # 一旦查到就不结束,默认是从下标0开始
print(info.find('x')) # 如果没有这个元素,返回是-1

if info.find('a')!= -1:
    print('存在')

# 7.join 一般是列表与元组
print(' * '.join(['I','like','play','football']))

# 8.split--分割--返回是列表
info = '  name is tom  '
print(info.split('m')) #1- 一定返回是列表  2、里面写切点 3- 切点没有了
info = 'nameistom'
print(info.split(' '))  # 切点不存在--返回整个
print(list(info)) #list字符串转列表

# 11.replace--替换
info = '  name is tom tom '
print(info.replace('tom','jack'))   # 默认全部替换
print(info.replace('tom','jack',1)) # 只替换第一个
print(info.replace('tom tom','tom jack')) # 只替换第二个
print(info)#替换-----另存的概念--原来对象不变

# 12.strip--字符串前置空格和后置空格删除
print(''.join(info.strip().split()))  # 删除前中后空格
# print(info.replace(' ',''))

# -------------------扩展-------------------

# expandtabs ,断句
test = 'username\temail\tpassword\n'
print(test.expandtabs())

# 当前输入是否是数字
test= '①' # 1,一,①
print(test.isdecimal())  # 只识别--2
print(test.isdigit())    # 只识别--2或者②
print(test.isnumeric())  # 识别任何数字--2 二 ②

# isspace---判断是否全是空格
test= '   '
print(test.isspace())


# 分割
test = 'testadadsfdfsgger'
print(test.partition('s')) # 以第一个分割
print(test.rpartition('s')) # 以最后一个分割
print(test.split('s'))
print(test.rsplit('s'))

str
print("留言信息")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值