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

Python学习进程(8)字符串內建函数的更多相关文章

  1. python基础(二)字符串內建函数详解

    字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,''或""或''' '''中间包含的内容称之为字符串特性:1.只能存放一个值2.不可变,只能重新赋值3.按照从 ...

  2. Python学习-第二天-字符串和常用数据结构

    Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...

  3. Python学习进程(15)常用内置函数

        本节介绍Python的一些常用的内置函数.     (1)cmp(x, y): cmp()函数比较 x 和 y 两个对象,并根据比较结果返回一个整数,如果 x<y,则返回-1:如果x&g ...

  4. Python学习进程(6)函数

        函数最重要的目的是方便我们重复使用相同的一段程序.     (1)函数的定义: 函数定义的简单规则: 1.函数代码块以 def 关键词开头,后接函数标识符名称和圆括号(): 2.任何传入参数和 ...

  5. python学习之初识字符串

    刚接触一门语言时,字符串是很容易遇到的, 例如要从读入或者写出, 字符串与数字间的转换等. 由于字符串, 列表和元组等类型具有一定的共性(由对象组成的序列,如字符串是字符序列), 在Python中统称 ...

  6. Python学习(八) —— 内置函数和匿名函数

    一.递归函数 定义:在一个函数里调用这个函数本身 递归的最大深度:997 def func(n): print(n) n += 1 func(n) func(1) 测试递归最大深度 import sy ...

  7. Python学习笔记——常用的内置函数

    一.yield def EricReadlines(): seek = 0 while True: with open('D:/temp.txt','r') as f: f.seek(seek) da ...

  8. Python学习进程(9)序列

    序列是Python中最基本的数据结构.     (1)序列简介: 序列中的每个元素都分配一个数字标明它的位置或索引,第一个索引是0,第二个索引是1,依此类推.序列都可以进行的操作包括索引,切片,加,乘 ...

  9. python中实现查找字符串的find函数

    第五题:自己实现一个字符串的find函数1.在一个字符串中查找另一个字符串2.找到了返回第一次出现的位置3.没找到返回-14.参数s1为源字符串,参数s2为要查找的字符串 def index_of_s ...

随机推荐

  1. MySQL 中文显示乱码

    最近关于中文显示乱码的贴子比较多,所以也做了个总结: 可以参考一下杨涛涛版主的<各种乱码问题汇总> http://topic.csdn.net/u/20071124/08/3b7eae69 ...

  2. Hibernate无主键配置文件编写

    1.       环境:jdk1.4+hibernate2.0+weblogic8 一般情况下,我们建的表都会有主键,然后根据hibernate的配置文件编写条件 有一个主键key,剩下的是Prope ...

  3. android模拟器怎么直接安装apk

    方法一:进入adb的tools目录,下面有adb.exe如图, 在cmd下进入tools目录,然后输入adb install apk路径,就行了 方法二:直接写一个批处理文件 运行就行了.

  4. ORACL EXP导出数据说明

    转载自:http://www.jb51.net/article/17358.htm Oracle 数据库导出(exp)导入(imp)说明   exp 将数据库内的各对象以二进制方式下载成dmp文件,方 ...

  5. linux系统启动过程具体解释-开机加电后发生了什么 --linux内核剖析(零)

    本文參考了例如以下文章 深入理解linux启动过程 mbr (主引导记录(Master Boot Record)) 电脑从开机加电到操作系统main函数之前执行的过程 详细解释linux系统的启动过程 ...

  6. FinalShell Mac OS版,Linux版安装及教程

    该版本功能和windows版基本一样,但是主机检测和远程桌面功能由于兼容性问题暂时无法使用,以后会支持. 该版本功能和windows版基本一样,但是主机检测和远程桌面功能由于兼容性问题暂时无法使用,以 ...

  7. SQL Server 日期格式化输出

    转自:http://hi.baidu.com/%BC%D1%C0%D6%B1%C8%BA%A3/blog/item/fdaf6c9525adfa0f7af480ec.html T-SQL Script ...

  8. Android系统中Parcelable和Serializable的区别,自动化实现Parcelable接口的插件

    Parcelable和Serializable的区别 参考地址:http://greenrobot.me/devpost/android-parcelable-serializable/ 由于最终的区 ...

  9. coursera 《现代操作系统》 -- 第七周 存储模型(1)

    虚拟地址 隔离进程,便于管理. 问:为什么不直接划分物理地址为一块一块,直接管理,而要做一层虚拟地址的映射呢? 栈和堆 Differences between Stack and Heap Stack ...

  10. setlocale(LC_ALL, ""); 取值为空字符串" "(注意,不是NULL),则locale与本地环境所使用的编码方式相同(在本地化时,应该很有用);

    在C运行库提供的多字节字符-宽字符转换函数:mbstowcs()/wcstombs()中,需要用到全局变量locale( locale encoding ),以指定多字节字符的编码类型 1. 功能: ...