【Python】字符串处理
2012-12-27 09:35
113 查看
对于每一种语言,最重要的部分之一可能莫过于对字符串的处理
所以,原本打算将这些基础的函数弄在同一个博文里的,但是因为字符串处理的重要性
所以,打算单独弄出一篇整理笔记。
参照网上博文http://blog.csdn.net/wklken/article/details/6312870等
字符串内建函数
特殊字符串,以大写R 或小写r开始,为正则表达式设计的,其中\不再表示转义
【用于处理正则表达式,减少反斜杠】
path = r'e:\book'
可以方便地表示系统路径
%d 十进制整数
%o 八进制整数
%s 字符串
%x 十六进制整数,其中字母小写
%X 十六进制整数,其中字母大写
str = "so %s a day!"
str % 'beautiful'
“%s,a,b” %”TOM” 格式化
‘{0} is {1}’.format(‘a’,’b’)
%[(name)][flags][width][,precision]typecode
-左对齐 +正负号 0补0
%-6d六位左对齐
d六位补零
基于字典的格式化
“%(n)d %(x)x” % {“n”:1 , “x”:spam}
python2.6之后格式化新方法:
可以通过位置或关键字进行填充
template = “{0}, {1} and {2}”
template.format(‘spam’,’ham’,’eggs’)
template = “{motto},{pork} and food”
template.format (motto=”a”,port = ‘ham’,food=’eggs’)
template = “{motto}, {0} and {food}”
template.format(‘ham’,motto=”a”,food=”b”)
存在其他更强大的用法,参见p198
格式化字符串:
pattern = “%s -----%s”
c = parttern %(a,b) #格式化后总返回新的字符串
正则表达式
有待补充
所以,原本打算将这些基础的函数弄在同一个博文里的,但是因为字符串处理的重要性
所以,打算单独弄出一篇整理笔记。
参照网上博文http://blog.csdn.net/wklken/article/details/6312870等
字符串内建函数
string.capitalize() | 字符串第一个字符大写 |
string.center(width,[,fill]) | 原字符居中,空格填充至width长度 |
string.count(str,beg=0,end=len(string)) | 获得字符串中某一个子串的数目,计算出现次数,可指定范围 |
string.decode(encoding=’UTF-8’,errors=’strict’) | 解码字符串,出错默认报ValueError,除非errors是ignore或replace |
string.encode(encoding=’UTF-8’,errors=’strict’) | |
string.endswith(suffix,beg=0,end=len(string)) | 是否以**结尾 |
string.expandtabs(tabsize=8) | 把字符串中tab转为空格,默认8个 |
string.find(str,beg=0,end=len(stirng)) | 检测是否包含str,存在返回开始索引,否则返回-1 |
string.index(str,begin=0,end=len(string)) | 同find,不存在报异常 |
string.isalnum() | 至少一个字符,且所有字符均为字母或数字,True. 检测字符串是否只包含0-9A-Za-z |
string.isalpha() | 至少一个字符,所有字符都是字母,True. 检测字符串是否只包含字母 |
string.isdecimal() | 只包含十进制数,True |
stirng.isdigit() | 只包含数字,True. 检测字符串是否仅包含数字 |
string.islower() | 至少一个区分大小写字符且所有字符小写,True. 检测字符串是否均为小写字母 |
string.isnumeric() | 只含数字字符,True |
string.isspace() | 只包含空格,True. 检测字符串是否均为空白字符 |
string.istitle() | 标题化字符,True. 检测字符串中的单词是否为首字母大写 |
string.isupper() | 至少一个区分大小写字符且所有字符大写,True. 检测字符串是否均为大写字母 |
string.join(seq) | 以string作为分隔符,seq中所有元素合并为新的字符串. 将原字符串插入参数字符串中的每两个字符之间 |
string.ljust(width) | 返回一个原字符串左对齐,空格补充至长度width |
string.lower() | 转小写. 将字符串全部转为小写 |
string.lstrip() | 截掉左侧的空格 |
string.partition(str) | = find+split,从str出现第一个位置,截断为pre_str,str,after_str元组,不含str则pre_str=str |
string.replace(str1,str2,num=string.count(str1)) | 替换,指定不超过num次,可作为模板实现 |
string.rfind(str,beg=0,end=len(string)) | 同find,右边开始 |
string.rindex(str,beg=0,end=len(string)) | 同index,右边开始 |
string.rjust(width) | 右对齐,空格补齐 |
string.rpartition(str) | 同partition,右边开始 |
string.rstrip([chars]) | 清理右侧空白,包括换行符,返回处理后字符串 |
string.split(str=””, maxsplit =string.count(str)) | 以str切片,可指定分割次数, 分割字符串,返回列表,默认分隔符空格 |
string.splitlines(num=string.count(‘\n’)) s.splitlines([keepends]) | 按行分隔,可指定分割次数 |
string.startswith(obj,beg=0,end=len(string)) | 以str开头,True. 检测字符串是否以某一子串开头 |
string.strip([obj]) | 在string上执行lstrip和rstrip |
string.swapcase | 反转string中大小写. 字符串中小写转大写,大写转小写 |
string.title() | 标题花,单词首字母大写,其余小写 |
string.translate(str,del=””) s.translate(table) | 根据str给出表转换string字符,要过滤的字符放在del参数中 |
string.upper() | 转大写. 将字符串全部转为大写 |
string.zfill(width) | 返回长度width的字符串,原字符串右对齐,前面填充0 |
len(string) | 获取字符串的长度 |
原始字符串(raw string)
原始字符串常量,r”abcd”,即去掉了反斜线转义机制。关闭转义机制,常见于正则表达式特殊字符串,以大写R 或小写r开始,为正则表达式设计的,其中\不再表示转义
【用于处理正则表达式,减少反斜杠】
path = r'e:\book'
可以方便地表示系统路径
格式化输出字符串
%c 单个字符%d 十进制整数
%o 八进制整数
%s 字符串
%x 十六进制整数,其中字母小写
%X 十六进制整数,其中字母大写
str = "so %s a day!"
str % 'beautiful'
“%s,a,b” %”TOM” 格式化
‘{0} is {1}’.format(‘a’,’b’)
%[(name)][flags][width][,precision]typecode
-左对齐 +正负号 0补0
%-6d六位左对齐
d六位补零
基于字典的格式化
“%(n)d %(x)x” % {“n”:1 , “x”:spam}
python2.6之后格式化新方法:
可以通过位置或关键字进行填充
template = “{0}, {1} and {2}”
template.format(‘spam’,’ham’,’eggs’)
template = “{motto},{pork} and food”
template.format (motto=”a”,port = ‘ham’,food=’eggs’)
template = “{motto}, {0} and {food}”
template.format(‘ham’,motto=”a”,food=”b”)
存在其他更强大的用法,参见p198
格式化字符串:
pattern = “%s -----%s”
c = parttern %(a,b) #格式化后总返回新的字符串
正则表达式
有待补充
相关文章推荐
- Python内置的字符串处理函数整理
- Python 中的字符串处理函数
- python处理中文字符串的问题
- python用的比较多的字符串处理函数
- python-字符串处理-字符串'转'JSON并取值
- python字符串处理
- Python字符串处理
- Python 字符串处理操作
- Python中处理字符串之isalpha()方法的使用
- Python 重要的字符串处理函数
- Python 第二章 字符串及其相关处理
- Python内置的字符串处理函数详细整理(覆盖日常所用)
- 第二章 Python字符串处理和编码不再发愁
- Python内置的字符串处理函数整理
- python小实验(1):字符串处理
- Python内置的字符串处理函数整理
- Python2和Python3之间关于字符串编码处理的差别
- Data Analysis学习笔记 --- Python数据清洗对字符串处理
- python处理中文字符串
- python cookbook 读书笔记2(字符串处理2)