python string模块内建函数
2015-09-15 14:18
537 查看
>>> dir(string)
['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error',
'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits',
'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']
字符串类型内建方法
方法 描述
string.capitalize()
把字符串的第一个字符大写
>>> import string
>>> string.capitalize('bcd')
'Bcd'
string.center(width)
返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
>>>str_1='bcd'
>>> str_1.center(5)
' bcd '
string.count(str, beg=0,end=len(string))返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则
返回指定范围内 str 出现的次数
str_1='this is a string example'
sub='i'
print str_1.count(sub,4,24)
sub='this'
print str_1.count(sub,0,24)
>>>
2
1
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'
str = "this is string example"
str = str.encode('base64','strict')
print "Encoded String: " + str,
print "Decoded String: " + str.decode('base64','strict')
>>>
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZQ==
Decoded String: this is string example
string.endswith(obj, beg=0,end=len(string))检查字符串是否以 obj 结束,如果 beg 或者 end 指定则检查指
定的范围内是否以 obj 结束, 如果是, 返回 True,否则返回 False.
str = "this is string example"
suffix = "ple"
print str.endswith(suffix)
print str.endswith(suffix,20)
suffix = "is";
print str.endswith(suffix, 2, 4);
print str.endswith(suffix, 2, 6);
>>>
True
False
True
False
string.expandtabs(tabsize=8)把字符串 string 中的 tab 符号转为空格, 默认的空格数 tabsize 是 8.
str = "this is\tstring example"
print "Original string: " + str
print "Defualt exapanded tab: " + str.expandtabs()
print "Double exapanded tab: " + str.expandtabs(16)
>>>
Original string: this is string example
Defualt exapanded tab: this is string example
Double exapanded tab: this is string example
string.find(str, beg=0,end=len(string))检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
str = "this is\tstring example"
sub = 'is'
print str.find(sub)
print str.find(sub,7,22)
>>>
2
-1
string.index(str, beg=0,end=len(string)) 跟 find()方法一样,只不过如果 str 不在 string 中会报一个异常.
string.isalnum() 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
str = "this2009" # No space in this string
print str.isalnum()
str = "this is string example....wow!!!"
print str.isalnum()
>>>
True
False
string.isalpha() 如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
string.isdecimal() 如果 string 只包含十进制数字则返回 True 否则返回 False.
string.isdigit() 如果 string 只包含数字则返回 True 否则返回 False.
string.islower() 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小 写,则返回 True,否则返回 False
string.isnumeric() 如果 string 中只
4000
包含数字字符,则返回 True,否则返回 False
string.isspace() 如果 string 中只包含空格,则返回 True,否则返回 False.
string.istitle() 如果 string 是标题化的(见 title())则返回 True,否则返回 False
string.isupper() 如果 string 中包含至少一个区分大小写的字符, 并且所有这些(区分大小写的)字符都是大 写,则返回 True,否则返回
False
string.join(seq)
Merges (concatenates)以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
str = "-"
seq = ("a", "b", "c")# This is sequence of strings.
print str.join( seq )
>>>
a-b-c
string.ljust(width) 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
string.lower() 转换 string 中所有大写字符为小写.
string.lstrip() 截掉 string 左边的空格
string.partition(str) 有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个3 元 素 的 元 组(string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str
== string.
string.replace(str1, str2,num=string.count(str1))把 string 中的 str1 替换成 str2,如果 num 指定,
则替换不超过 num 次.
string.rfind(str, beg=0,end=len(string))类似于 find()函数, 不过是从右边开始查找.
string.rindex( str, beg=0,end=len(string)) 类似于 index(), 不过是从右边开始.
string.rjust(width)返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
string.rpartition(str)e 类似于 partition()函数,不过是从右边开始查找.
string.rstrip()删除 string 字符串末尾的空格.
string.split(str="", num=string.count(str)) 以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串
string.splitlines(num=string.count('\n'))按照行分隔, 返回一个包含各行作为元素的列表, 如果 num 指定则仅切片 num 个行.
string.startswith(obj, beg=0,end=len(string))检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.
string.strip([obj]) 在 string 上执行 lstrip()和 rstrip()
string.swapcase() 翻转 string 中的大小写
string.title() 返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
string.translate(str, del="") 根据 str 给出的表(包含 256 个字符)转换 string 的字符,要过滤掉的字符放到 del 参数中
string.upper() 转换 string 中的小写字母为大写
string.zfill(width) 返回长度为 width 的字符串,原字符串 string 右对齐,前面填充
['Formatter', 'Template', '_TemplateMetaclass', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_float', '_idmap', '_idmapL', '_int', '_long', '_multimap', '_re', 'ascii_letters', 'ascii_lowercase', 'ascii_uppercase', 'atof', 'atof_error',
'atoi', 'atoi_error', 'atol', 'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 'expandtabs', 'find', 'hexdigits', 'index', 'index_error', 'join', 'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 'octdigits',
'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 'upper', 'uppercase', 'whitespace', 'zfill']
字符串类型内建方法
方法 描述
string.capitalize()
把字符串的第一个字符大写
>>> import string
>>> string.capitalize('bcd')
'Bcd'
string.center(width)
返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
>>>str_1='bcd'
>>> str_1.center(5)
' bcd '
string.count(str, beg=0,end=len(string))返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则
返回指定范围内 str 出现的次数
str_1='this is a string example'
sub='i'
print str_1.count(sub,4,24)
sub='this'
print str_1.count(sub,0,24)
>>>
2
1
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'
str = "this is string example"
str = str.encode('base64','strict')
print "Encoded String: " + str,
print "Decoded String: " + str.decode('base64','strict')
>>>
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZQ==
Decoded String: this is string example
string.endswith(obj, beg=0,end=len(string))检查字符串是否以 obj 结束,如果 beg 或者 end 指定则检查指
定的范围内是否以 obj 结束, 如果是, 返回 True,否则返回 False.
str = "this is string example"
suffix = "ple"
print str.endswith(suffix)
print str.endswith(suffix,20)
suffix = "is";
print str.endswith(suffix, 2, 4);
print str.endswith(suffix, 2, 6);
>>>
True
False
True
False
string.expandtabs(tabsize=8)把字符串 string 中的 tab 符号转为空格, 默认的空格数 tabsize 是 8.
str = "this is\tstring example"
print "Original string: " + str
print "Defualt exapanded tab: " + str.expandtabs()
print "Double exapanded tab: " + str.expandtabs(16)
>>>
Original string: this is string example
Defualt exapanded tab: this is string example
Double exapanded tab: this is string example
string.find(str, beg=0,end=len(string))检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
str = "this is\tstring example"
sub = 'is'
print str.find(sub)
print str.find(sub,7,22)
>>>
2
-1
string.index(str, beg=0,end=len(string)) 跟 find()方法一样,只不过如果 str 不在 string 中会报一个异常.
string.isalnum() 如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
str = "this2009" # No space in this string
print str.isalnum()
str = "this is string example....wow!!!"
print str.isalnum()
>>>
True
False
string.isalpha() 如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
string.isdecimal() 如果 string 只包含十进制数字则返回 True 否则返回 False.
string.isdigit() 如果 string 只包含数字则返回 True 否则返回 False.
string.islower() 如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小 写,则返回 True,否则返回 False
string.isnumeric() 如果 string 中只
4000
包含数字字符,则返回 True,否则返回 False
string.isspace() 如果 string 中只包含空格,则返回 True,否则返回 False.
string.istitle() 如果 string 是标题化的(见 title())则返回 True,否则返回 False
string.isupper() 如果 string 中包含至少一个区分大小写的字符, 并且所有这些(区分大小写的)字符都是大 写,则返回 True,否则返回
False
string.join(seq)
Merges (concatenates)以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串
str = "-"
seq = ("a", "b", "c")# This is sequence of strings.
print str.join( seq )
>>>
a-b-c
string.ljust(width) 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
string.lower() 转换 string 中所有大写字符为小写.
string.lstrip() 截掉 string 左边的空格
string.partition(str) 有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个3 元 素 的 元 组(string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str
== string.
string.replace(str1, str2,num=string.count(str1))把 string 中的 str1 替换成 str2,如果 num 指定,
则替换不超过 num 次.
string.rfind(str, beg=0,end=len(string))类似于 find()函数, 不过是从右边开始查找.
string.rindex( str, beg=0,end=len(string)) 类似于 index(), 不过是从右边开始.
string.rjust(width)返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
string.rpartition(str)e 类似于 partition()函数,不过是从右边开始查找.
string.rstrip()删除 string 字符串末尾的空格.
string.split(str="", num=string.count(str)) 以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串
string.splitlines(num=string.count('\n'))按照行分隔, 返回一个包含各行作为元素的列表, 如果 num 指定则仅切片 num 个行.
string.startswith(obj, beg=0,end=len(string))检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.
string.strip([obj]) 在 string 上执行 lstrip()和 rstrip()
string.swapcase() 翻转 string 中的大小写
string.title() 返回"标题化"的 string,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())
string.translate(str, del="") 根据 str 给出的表(包含 256 个字符)转换 string 的字符,要过滤掉的字符放到 del 参数中
string.upper() 转换 string 中的小写字母为大写
string.zfill(width) 返回长度为 width 的字符串,原字符串 string 右对齐,前面填充
相关文章推荐
- 【9-15】python学习笔记01
- python中的字典详细介绍
- python中使用sys.argv命令行参数
- 使用python遍历目录浏览文件属性
- 每日python (8)
- python中编码
- 每日python(7)
- 实验一个最小的PYTHON服务器编程
- 零基础学python-16.7 nonlocal介绍
- 零基础学python-16.7 nonlocal介绍
- Unix时间戳转换(python)
- 数学思想方法-python计算战(8)-机器视觉-二值化
- Python+OpenCV学习(13)---meanshift目标跟踪
- Python一个有意思的地方:reduce、map、filter
- 使用python将ppm格式转换成jpg
- python程序运行时间
- 不错的python中级电子书
- alembic 使用
- 转:python命令行解析工具Argparse
- python核心编程第5章课后题答案