Python字符串內建函数实现了string模块的大部分方法,并包括了对Unicode编码方式的支持。

    (1)capitalize():

将字符串的第一个字母变成大写,其他字母变小写。对于 8 位字节编码需要根据本地环境。

>>> str='I AM MenAngel!'+'I am Student!'
>>> print(str)
I AM MenAngel!I am Student!
>>> str.capitalize()
'I am menangel!i am student!'

    (2)center(width):

返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格。

>>> str='title:Python and Big data!'
>>> str
'title:Python and Big dat!'
>>> str.center(40)
' title:Python and Big dat! '
>>>

    (3)count(sub, start= 0,end=len(string)):

用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。

>>> str='xiaoming and MenAngel and xiaohua'
>>> str.count('and',10,20)
0
>>> str.count('and',0,len(str))
2

    (4)encode(encoding='UTF-8',errors='strict'):

以 encoding 指定的编码格式编码字符串。默认编码为字符串编码。该方法返回编码后的字符串。

参数:

encoding -- 要使用的编码,如"UTF-8"。
errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。

实例:

>>> str='This is a string example!'
>>> str=str.encode('utf-8','strict')
>>> print(str)
b'This is a string example!'

encode解码用字符串编码方式的字符串,返回bytes类型。并且不能再次被解码:

 str=str.encode('base','strict')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'bytes' object has no attribute 'encode'

    (5)deconde():

以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。

参数与(4)类似:

>>> str='This is a string example!'
>>> str=str.encode('utf-8','strict')
>>> print(str)
b'This is a string example!'
>>> str=str.decode('utf-8','strict')
>>> print(str)
This is a string example!
>>> str
'This is a string example!'

    (6)endswith(suffix[, start[, end]]):

endswith() 方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数"start"与"end"为检索字符串的开始与结束位置。

参数:

suffix -- 该参数可以是一个字符串或者是一个元素。
start -- 字符串中的开始位置。
end -- 字符中结束位置。

如果字符串含有指定的后缀返回True,否则返回False。

>>> str='上海自来水来自哪里啊?上海!'
>>> suffix='上海'
>>> str.endwith(suffix,0,length(str)-1)
>>> str.endswith(suffix,0,len(str)-1)
False
>>> str.endswith(suffix,0,2)
True
>>> str[0:len(str)-1]
'上海自来水来自哪里啊?上'
>>> str
'上海自来水来自哪里啊?上海'
#结论:叹号不再字符串里

    (7)expandtabs(tabsize=4):

    把字符串中的 tab 符号('\t')转为空格,tab 符号('\t')默认的空格数是 4。该方法返回字符串中的 tab 符号('\t')转为空格后生成的新字符串。

>>> print(str)
你好啊 MenAngel
>>> print(str.expandtabs(10))
你好啊 MenAngel
>>> print(str.expandtabs(0))
你好啊MenAngel
>>> print(str) #str并未改变,此函数创建了一个副本
你好啊 MenAngel

    (8)find(str, beg=0, end=len(string)):

检测字符串中是否包含子字符串 str ,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串返回开始的索引值,否则返回-1。

>>> str='loyalty is the foundation on which the night\'s wath was built'
>>> str
"loyalty is the foundation on which the night's wath was built"
>>> print(str.find('is',5))
8
>>> print(str.find('xiaoming',0,len(str)-5))
-1

    (9)find(str, beg=0, end=len(string))

检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。

参数:

str -- 指定检索的字符串
beg -- 开始索引,默认为0。
end -- 结束索引,默认为字符串的长度。

实例:(find与index的区别)

>>> str
"loyalty is the foundation on which the night's wath was built"
>>> str.index('is')
8
>>> str.index('Men')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> str.find('Men')
-1

最新文章

  1. Docker之Compose服务编排
  2. 详解JS跨域问题
  3. MFC字符串转化成16进制
  4. POJ 3345 Bribing FIPA 树形DP
  5. Generate the Jobs script from msdb Database
  6. 使用dxNavBar动态创建应用程序菜单
  7. 李洪强iOS开发支付集成之支付宝支付
  8. 网络爬虫-url索引
  9. ZOJ-2587-Unique Attack(最小割的唯一性)
  10. ECMAscript6新特性之解构赋值
  11. 速微共享链的使用步骤和源码分析(UI设计参考)
  12. BizTalk日志自动发送邮件通知
  13. HDU1847 Good Luck in CET-4 Everybody 博弈 SG函数
  14. cmd下【java监视和管理控制台】
  15. 5 -- Hibernate的基本用法 --1 ORM和Hibernate
  16. 浅谈实体类为什么要实现Serializable接口?
  17. 面向对象课程 - 寒假第三次作业 - C++计算器项目初始部分
  18. notify notifyAll 死锁
  19. 在ubuntu14.04上漏洞环境vulndocker的DOCKER搭建
  20. Factory Method(工厂方法)

热门文章

  1. centos修改启动顺序,登录后提示,启动级别,主机名,免密登录
  2. 【翻译自mos文章】在Oracle GoldenGate中循环使用ggserr.log的方法
  3. JetNuke笔记 ( by quqi99 )
  4. Tomcat无法启动:Server Tomcat v8.5 Server at localhost failed to start
  5. 我们可以用JAX-WS轻松实现JAVA平台与其他编程环境(.net等)的互操作
  6. go语言递归创建目录
  7. CodeIgniter框架——CI的执行流程
  8. [Spring Data MongoDB]学习笔记--注册一个Mongo实例
  9. 前台传递给后台的JSON字符串中的引号 “” 在JAVA后台被转义为 &quot
  10. 巨蟒python全栈开发linux之cento8