python中的字符串处理
来源:互联网 发布:求实软件2016 编辑:程序博客网 时间:2024/05/23 01:23
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
- Python中的字符串处理
- python 中的字符串处理
- python中的字符串处理
- Python中的字符串处理
- Python中的字符串处理
- Python中的字符串处理
- python中的字符串处理函数
- Python 中的字符串处理函数
- python中的字符串处理replace函数
- 13、Python 之处理字符串中的空格
- 使用Python脚本处理OC中的中文字符串
- python中如何处理一个字符串中的每一个字符
- 使用自己的Python函数处理Protobuf中的字符串编码
- 在python中处理字符串中的空格和换行符
- 【python coding 3:字符串处理】python实现去除字符串中的错误
- python中文字符串处理
- python 字符串处理函数
- python字符串处理函数
- 基于CC2430使终端设备依据特定的PAN ID入网
- 创新实验室实习生每周工作总结【实习第五周】
- POJ 2965详解 (bfs、位运算)
- uva748 - Exponentiation
- hdu 1009FatMouse' Trade
- python中的字符串处理
- 2014/4/28~2014/5/4
- floyd求最小环 模板
- 二叉树 已知前序,中序求后序 TOJ 3988
- kafka环境搭建二---Windows客户端Linux服务器
- Linux内核开发之阻塞非阻塞IO----轮询操作
- The Inheritance Hierarchy of QT
- ZigBee各层重要属性注释
- mysql中间件研究(Atlas,cobar,TDDL)