python中的字符串处理
2017-07-29 20:31
141 查看
1.字符串转换
s.lower() 转为小写
s.upper() 转为大写
s.swapcase() 大写转为小写,小写转为大写
s.capitalize() 首字母大写
转换为int类型 string.atoi(s) 或者int(s)
转换为float类型 string.atof(s) 或者float(s)
转换为long类型 string.atol(s) 或者long(s)
2.查找等操作
s.find(sub,[,start[,end]]) 返回首次出现的位置。找不到返回-1
s.rfind(sub,[,start[,end]]) 返回最后一次出现的位置。找不到返回-1
s.index(sub[,start[,end]]) 与find()功能类似。找不到则传出ValueEerror
s.rindex(sub[,start[,end]]) 与rfind()功能类似,找不到则传出ValueError
s.count(sub[,start[,end]]) 返回子串出现的次数
s.replace(old,new[,maxreplace]) 替换字符串,指定maxreplace时。仅仅替换前maxreplace个
s.strip(char) 删除開始和结尾处的char
s.split([,seq[,maxsplit]]) 返回切割字符串的列表
s.join([sep]) 连接字符串
3.位置
s.ljust(width[,fillchar]) 左对齐
s.rjust(width[,fillchar]) 右对齐
s.center(width[,fillchar]) 居中
s.zfill(width) 左边补零直到长度到width
4.格式化输出
format能够改变字符串的输出形式,举例为:
‘{0},{2},{1}’.format(‘a’,’b’,’c’)
这里{0} {1} {2}分别指代’a’ ‘b’ ‘c’
也能够依照名称来写:
‘cordix:{x},{y}’.format(x=’1’,y=’2’)
字符串的左对齐也能够用format
‘{:<10}’.format(“hello”) 左对齐,宽度为10
‘{:>10}’.format(“hello”) 右对齐。宽度为10
‘{:^10}’.format(“hello”) 居中,宽度为10
s.lower() 转为小写
s.upper() 转为大写
s.swapcase() 大写转为小写,小写转为大写
s.capitalize() 首字母大写
转换为int类型 string.atoi(s) 或者int(s)
转换为float类型 string.atof(s) 或者float(s)
转换为long类型 string.atol(s) 或者long(s)
2.查找等操作
s.find(sub,[,start[,end]]) 返回首次出现的位置。找不到返回-1
s.rfind(sub,[,start[,end]]) 返回最后一次出现的位置。找不到返回-1
s.index(sub[,start[,end]]) 与find()功能类似。找不到则传出ValueEerror
s.rindex(sub[,start[,end]]) 与rfind()功能类似,找不到则传出ValueError
s.count(sub[,start[,end]]) 返回子串出现的次数
s.replace(old,new[,maxreplace]) 替换字符串,指定maxreplace时。仅仅替换前maxreplace个
s.strip(char) 删除開始和结尾处的char
s.split([,seq[,maxsplit]]) 返回切割字符串的列表
s.join([sep]) 连接字符串
3.位置
s.ljust(width[,fillchar]) 左对齐
s.rjust(width[,fillchar]) 右对齐
s.center(width[,fillchar]) 居中
s.zfill(width) 左边补零直到长度到width
4.格式化输出
format能够改变字符串的输出形式,举例为:
‘{0},{2},{1}’.format(‘a’,’b’,’c’)
这里{0} {1} {2}分别指代’a’ ‘b’ ‘c’
也能够依照名称来写:
‘cordix:{x},{y}’.format(x=’1’,y=’2’)
字符串的左对齐也能够用format
‘{:<10}’.format(“hello”) 左对齐,宽度为10
‘{:>10}’.format(“hello”) 右对齐。宽度为10
‘{:^10}’.format(“hello”) 居中,宽度为10
相关文章推荐
- python字符串处理方法(一)
- python字符串处理
- python字符串处理常用方法
- Python字符串处理
- [笔记]--Python字符串处理
- python基础之流程控制、数字和字符串处理
- Python内置的字符串处理函数整理
- Python中处理字符串的相关的len()方法的使用简介
- Python处理字符串之isspace()方法的使用
- python编码处理:unicode字节串转成中文 各种字符串举例说明
- Python内置的字符串处理函数整理
- Python四钟方式来实现“一次处理一个字符的方式处理字符串”
- Python中的字符串处理
- [转]python中文字符串处理问题
- 通过Python的内置字符串处理函数来处理字符串的方法和整理
- Python字符串处理出现错误
- Python基础学习----字符串处理
- 轻松python文本专题-单独处理字符串每个字符的方法汇总
- python知识积累(三)字符串处理
- python字符串处理内置方法一览表