码迷,mamicode.com
首页 > 其他好文 > 详细

str函数

时间:2019-11-22 13:44:49      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:substring   数字   区别   error   使用   not found   ror   查找   unicode   


查找类函数
字符串查找类: 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

 

代码段:


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

str函数

标签:substring   数字   区别   error   使用   not found   ror   查找   unicode   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!