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

常用的字符串方法

时间:2017-06-08 22:36:12      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:返回   strip()   for   cas   index   连接字符串   find   替换   它的   

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 

 

常用的字符串方法

标签:返回   strip()   for   cas   index   连接字符串   find   替换   它的   

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

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