设为首页 收藏本站
查看: 1208|回复: 0

[经验分享] python string

[复制链接]

尚未签到

发表于 2018-8-5 06:53:15 | 显示全部楼层 |阅读模式
  方法  描述
  string.ljust(width)

  返回一个原字符串左对齐,并使用空格填充至长度>  \>>> x.ljust(20)
  'Hello,wold       ‘
  string.center(width)

  返回一个原字符串居中,并使用空格填充至长度>  \>>> x.center(20)
  '     Hello,wold     '
  \>>>
  string.rjust(width)

  返回一个原字符串右对齐,并使用空格填充至长度>  \>>> x.rjust(20)
  '          Hello,wold'
  string.zfill(width)

  返回长度为>  \>>> str.zfill(20)
  '000Book on the deskk'
  \>>> str.zfill(30)
  '0000000000000Book on the deskk'
  string.startswith(obj, beg=0,end=len(string))
  检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.
  string.endswith(obj, beg=0, end=len(string))
  检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.
  string.isalnum()
  isalnum() 方法检测字符串是否由字母和数字组成。
  如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
  \>>> str="this2010"
  \>>> str.isalnum()
  True
  \>>> str="this2010."
  \>>> str.isalnum()
  False
  \>>> str="this"
  \>>> str.isalnum()
  True
  \>>> str="123456"
  \>>> str.isalnum()
  True
  \>>>
  string.isalpha()
  如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
  \>>> str="this"
  \>>> str.isalpha()
  True
  \>>> str.isalpha()
  False
  \>>>
  string.isdigit()
  如果 string 只包含数字则返回 True 否则返回 False.
  \>>> a=2
  \>>> a.isdigit()
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  AttributeError: 'int' object has no attribute 'isdigit'
  \>>> str=&quot;123&quot;
  \>>> str.isdigit()
  True
  \>>> str=&quot;this is a example&quot;
  \>>> str.isdigit()
  False
  \>>> string.isdecimal()
  isdecimal()方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。
  \>>> str=u&quot;1234&quot;
  \>>> str.isdecimal()
  True
  \>>> str=u&quot;this2010&quot;
  \>>> str.isdecimal()
  False
  \>>>
  string.islower()
  如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
  \>>> str='i89ke'
  \>>> str.islower()
  True
  string.isupper()
  如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是大写,则返回 True,否则返回 False
  string.isnumeric()
  如果 string 中只包含数字字符,则返回 True,否则返回 False
  \>>> str=u'234'
  \>>> str.isnumeric()
  True
  \>>> str=u'this2010'
  \>>> str.isnumeric()
  False
  \>>>
  string.isspace()
  如果 string 中只包含空格,则返回 True,否则返回 False.
  \>>> str=&quot;    &quot;                            #至少包含一个以上空格
  \>>> str.isspace()
  True
  \>>> str=&quot;&quot;
  \>>> str.isspace()
  False
  \>>>
  string.istitle()
  如果字符串中所有的单词拼写首字母是否为大写,且其他字母为小写则返回 True,否则返回 False.
  \>>> str=&quot;Dont Do It&quot;
  \>>> str.istitle()
  True
  \>>> str=&quot;Dont do it&quot;
  \>>> str.istitle()
  False
  \>>>
  string.capitalize()
  把字符串的第一个字符大写
  \>>> s='a,B'
  \>>> s.capitalize()
  'A,b'
  \>>> str=&quot;this is very interintsting&quot;
  \>>> str.capitalize()
  'This is very interintsting'
  \>>>
  string.lower()
  转换 string 中所有大写字符为小写.
  \>>> s.lower()
  'a,b'
  string.upper()
  转换 string 中的小写字母为大写
  \>>> s.upper()
  'A,B'
  \>>>
  string.title()
  返回&quot;标题化&quot;的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
  \>>> str=&quot;This a book,And that?&quot;
  \>>> str.title()
  'This A Book,And That?'
  max(str)
  返回字符串 str 中最大的字母。
  min(str)
  返回字符串 str 中最小的字母。
  \>>> str=&quot;this is really a string example....wow!!&quot;
  \>>> max(str)
  'y'
  \>>> min(str)
  ' '
  \>>> len(str)
  40
  string.format()
  格式化字符串
  string.count(str, beg=0, end=len(string))
  返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数
  \>>> str=&quot;this is string example....wow!!&quot;
  \>>> sub='i'
  \>>> str.count(sub,4,40)
  2
  \>>> str.count('wow')
  1
  string.replace(str1, str2,  num=string.count(str1))
  把 string 中的 str1 替换成 str2,如果 num 指定,则替换不超过 num 次.
  返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。
  \>>> str=&quot;this is string example...wow!! this is really string&quot;
  \>>> str.replace('is','wa')
  'thwa wa string example...wow!! thwa wa really string'
  \>>> str.replace('is','wa','2')
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  TypeError: an integer is required
  \>>> str.replace('is','was',2)
  'thwas was string example...wow!! this is really string'
  \>>>
  string.rindex( str, beg=0,end=len(string))
  类似于 index(),不过是从右边开始.
  string.index(str, beg=0, end=len(string))
  跟find()方法一样,只不过如果str不在 string中会报一个异常.
  \>>> str=&quot;this is string example...woow!!&quot;
  \>>> str2=&quot;exam&quot;
  \>>> str.index(str2)
  15
  \>>> str.index(str2,14)
  15
  \>>> str.index(str2,16)
  Traceback (most recent call last):
  File &quot;<stdin>&quot;, line 1, in <module>
  ValueError: substring not found
  \>>>
  string.rfind(str, beg=0,end=len(string) )
  类似于 find()函数,不过是从右边开始查找.
  string.find(str, beg=0, end=len(string))
  检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
  Python find() 方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。
  \>>> str=&quot;this is string example...woow!!&quot;
  \>>> str2=&quot;exam&quot;
  \>>> str.find(str2)
  15
  \>>> str.find(str2,10)
  15
  \>>> str.find(str2,50)
  -1
  string.split(str=&quot;&quot;, num=string.count(str))
  以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串
  string.splitlines([keepends])
  按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
  string.join(seq)
  以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
  \>>> str=&quot;-&quot;
  \>>> seq=['a','b','c']
  \>>> str.join(seq)
  'a-b-c'
  \>>> str='8080'
  \>>> str.join(seq)
  'a8080b8080c'
  \>>>
  string.rpartition(str)
  类似于 partition()函数,不过是从右边开始查找.
  string.partition(str)
  有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 (string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str == string.
  返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串
  \>>> str=&quot;this is very interintsting&quot;
  \>>> str.capitalize()
  'This is very interintsting'
  \>>> x=&quot;is&quot;
  \>>> str.partition(x)
  ('th', 'is', ' is very interintsting')
  \>>> str.rpartition(x)
  ('this ', 'is', ' very interintsting')
  string.strip([obj])
  在 string 上执行 lstrip()和 rstrip()
  \>>> str=&quot;9999what8888&quot;
  \>>> str.strip('9')
  'what8888'
  \>>> str.strip('8')
  '9999what'
  \>>> str.strip('9''8')
  'what'
  \>>> str.strip('8''9')
  'what'
  string.lstrip()
  截掉 string 左边的空格
  \>>> str=&quot;9999what8888&quot;
  \>>> str.lstrip('9''w')
  'hat8888'
  string.rstrip()
  删除 string 字符串末尾的空格.
  \>>> str=&quot;9999what8888&quot;
  \>>> str.rstrip('8''t')
  '9999wha'
  string.swapcase()
  翻转 string 中的大小写
  \>>> str=&quot;This a book,And that?&quot;
  \>>> str.swapcase()
  'tHIS A BOOK,aND THAT?'
  string.translate(str, del=&quot;&quot;)
  根据 str 给出的表(包含 256 个字符)转换 string 的字符,
  要过滤掉的字符放到 del 参数中
  string.maketrans(intab, outtab])
  maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
  string.expandtabs(tabsize=8)
  把字符串 string 中的 tab 符号转为空格,tab 符号默认的空格数是 8。
  string.decode(encoding='UTF-8', errors='strict')
  以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除非 errors 指 定 的 是 'ignore' 或 者'replace'
  string.encode(encoding='UTF-8', errors='strict')
  以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-546684-1-1.html 上篇帖子: python list 下篇帖子: python标准模块shlex
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表