str函数


查找类函数
字符串查找类: find,index
  find: 查找字符串中是否包含一个子串
  index:跟find的区别是,index找不到会抛异常
  rfind / lfind 从左/右开始查找
判断类函数
  此类函数的特点是一般是用is开头,如 islower
  isalpha 判断是否是字母
    - 前提是字符串至少包含一个字符
    - 汉字被认为是alpha. 区分中英文使用Unicode码
  isdigit,isnumeric,isdecimal 三个判断数字的函数
  islower 判断字符串是否是小写
内容判断类
  startswith / endswith 是否以xxx开头/结尾 。检测某个字符串是否以某个子串开头,常用3个参数
  suffix:被检查字符串,必须有
  start:检查的开始范围
  end:检查的结束范围
操作类函数
  format 格式化函数
  strip 这个函数的主要作用是删除字符串两边的空格
  lstrip / rstrip

  join 字符串拼接在一起

代码段:


a = "abcdwef"
b = a.find('we',3) #从第3位开始找
print(b)
4


a = "abcdwef"
b = a.index('we',6)
print(b)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-a3f3e7608f58> in <module>
1 a = "abcdwef"
----> 2 b = a.index('we',6)
3 print(b)

  ValueError: substring not found

a = 'abc'
b = a.islower()
print(b)
True


a = 'abd awawd'
b = 'abd awawd awafwaf awa afaw'
print(b.startswith(a))
print(b.endswith(a))

False
False

原文地址:https://www.cnblogs.com/mini-test/p/11910895.html