22-字符串的常用操作方法

```python
# 查找方法 有两个。一是:find()函数,如果查找不到则返回 -1。二是:index()函数,如果查找不到,则报错
# 返回的值是:所查找的字符串的第一个字符的下标,下标从0开始

# 1: find函数语法:变量.find('所查找的字符串')
str_find = 'hello world and python and zxy'
print(str_find.find('and'))  # 返回值: 12
# 1.1 rfind函数用法,从右侧开始查找,从整个字符串中查找到所需要的字符串的最右边的位置,但是显示的下标还是从左边0起始位置开始计数的,rindex函数同理
str_find = 'hello world and python and zxy'
print(str_find.rfind('and'))  # 返回值: 23

# 2:find()函数,如果查找不到则返回 -1
str_find = 'hello world and python and zxy'
print(str_find.find('worlds'))  # 返回值: -1

# 3:index()函数,变量.index('所查找的字符串')
str_index = 'hello world and python and zxy'
print(str_index.index('o'))

# 4:index()函数,如果查询不到则程序报错
str_index = 'hello world and python and zxy'
# print(str_index.index('o0'))

# 5:count()函数,返回所查找的字符串在整个字符串中出现的次数
str_count = 'hello world and python and zxy'
print(str_count.count('o'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值