python-str内置函数

str内置函数很多语言字符串使用string表示,python里面使用str表示字符串查找类,find,index,islower(判断小写)find:查找字符串中是否包括一个字符串index:跟find的唯一区别是index如果找不到会引发异常rfind,lfind:从左开始查或者从右开始查s = "Zhao min is very beautiful and Zou kexin...
摘要由CSDN通过智能技术生成

str内置函数

  • 很多语言字符串使用string表示,python里面使用str表示
  • 字符串查找类,find,index,islower(判断小写)
  • find:查找字符串中是否包括一个字符串
  • index:跟find的唯一区别是index如果找不到会引发异常
  • rfind,lfind:从左开始查或者从右开始查
s = "Fen fen is very beautiful and Zou kexin is very fascinating"
s1 = "Fen fen"
# 返回第一次发现这个字符串的位置
s.find(s1)

s2 = "xiaofen"
# 返回-1表示没有找到
s.find(s2)
-1

注意区别

help(str.find)
Help on method_descriptor:

find(...)
    S.find(sub[, start[, end]]) -> int
    
    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Return -1 on failure.
help(str.index)
Help on method_descriptor:

index(...)
    S.index(sub[, start[, end]]) -> int
    
    Return the lowest index in S where substring sub is found, 
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.
    
    Raises ValueError when the substring is not found.
# index 会报错或者引发异常
s.index(s2)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值