首页 > 其他 > 详细

常用的字符串方法

时间:2017-06-08 22:20:07      阅读:322      评论:0      收藏:0      [点我收藏+]

1. s.find(t): 字符串s中包含t的第一个索引(没找到返回-1)

2. s.rfind(t): 字符串s中包含t的最后一个索引(没找到返回-1)

3. s.index(t): 与s.find(t)功能类似,但没找到引起ValueError

4. s.rindex(t): 余s.rfind(t)功能类似,但没找到引起ValueError

5. s.join(text): 连接字符串s与text中的词汇

6.s.split(t): 在所有找到t的位置将s分割成链表(默认为空白符)

7. s.splitlines(): 将s按行分割成字符串链表

8.s.lower(): 将字符串s小写

9. s.upper(): 将字符串s大写

10. s.titlecase(): 将字符串s首字母大写

11. s.strip(): 返回一个没有首尾空白字符的s的复制

12. s.replace(t, u): 用u替换s中的t

 

下面介绍一个不常用的包,它的功能是防止行尾溢出:

textwrap模块

>>> from textwrap import fill
>>> format=%s (%d),
>>> pieces=[format % (word, len(word)) for word in saying]
>>> output=  .join(pieces)
>>> wrapped=fill(output)
>>> print wrapped 

 

常用的字符串方法

原文:http://www.cnblogs.com/no-tears-girl/p/6964841.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!