Python string模块 字符串函数方法操作教程
我们在Python编程过程中,经常会处理一些字符串的相关操作,例如:查找、替换、分隔、截取以及英文的大小写转换等,这个时候Python程序员首选的一定是 string模块 。
- string模块目录
一、string模块简单介绍
它适用于Python1.4以后的版本,当然现在大家一般都在用2.5以上的版本了,string模块基本上可以解决我们需要操作字符串的所有需要。
二、string模块字符串处理方法
玩蛇网测试环境:
系统:Window 7 64位
软件:IDLE (Python GUI)
版本:Python 2.7.6
1 )、英文字符串的大小写转换
>>> import string
#注意python2.0以后的版本, string模块不需要再调用了,它的方法改成用S.method()的形式调用,只要变量S是一个字符串对象就可以可以直接使用它的函数方法,不需要额外的 import 再导入。
>>>
>>> s = "IplayPython"
>>> print s
IplayPython
>>>
>>> s.lower() # 全部字符串转换为小写
'iplaypython'
>>> s.upper() #全部字符串转换为大写
'IPLAYPYTHON'
>>>
>>> s.capitalize() #首字母大写
'Iplaypython'
>>> s.swapcase() # 大小写互换
'iPLAYpYTHON'
>>>
还有一个首字母大写的方法叫做 s.title(),大家有兴趣的话可以去测试一下,看一下官方文档,它与上面的首字母大写的capitalize()方法有什么不同之处。
2 )、查找统计和替换字符串
>>> s = "IplayPython" #创建字符串
>>>
>>> s.find('I') # 用find方法来查找子串‘I’,如果找到就返回匹配的第一个索引值。
0
>>> s.find('P') # 这个同上
5
>>> s.find('k') # 我们查找字母‘K’,s字符串中没有,所以返回了一个 -1。
-1
>>>
另一个方法,叫做 index()方法,与上面功能类似,但是它如果查找的子串不存在,会直接报错,抛出Python异常,rindex()方法可以返回匹配的最后一次的索引值。
>>> s = "IplayPython"
>>>
>>> s.count('p') # 查找子串在字符串中的匹配次数
1
>>>
>>> s.replace("play", "love") #字符串替换方法,前面第一个参数放需要替换的子串,后面放替换后的内容。
'IlovePython'
>>>
>>> s = "0IplayPython0"
>>> s.strip('0') # strip()方法作用是删除头部和尾部的匹配字符。
'IplayPython'
>>>
strip()方法还有几个近亲,lstrip()与rstrip(),左边匹配删除,和右边匹配删除,一般我们用这些功能是删除空格、回车等。
三 、string模块使用注意事项
上面玩蛇网介绍了string模块的一些最基本最常用的方法,以后我们会更新更多的关于这个模块的方法和教程。
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/module/string.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速将两个队列变成字典
PYTHON基础入门必备教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
PYTHON进阶提高必备教程 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • Python linecache模块缓存读取大文件指定行
- • Python OS模块常用功能 中文图文详解
- • Python json解析模块loads/dumps中文encode教程
- • Python random模块sample、randint、shuffle、cho
- • Python urllib2模块post/get 下载网络资源
- • Python re正则表达式操作指南
- • Python time模块 函数格式 时间操作源码演示
- • Python Gzip压缩与解压模块
- • 第三方时间日期库 Python Arrow模块
- • Python Queue模块 多线程安全 先进先出的实
图文精华 RECOMMEND
-
Python linecache模块缓存读取大文件
-
Python OS模块常用功能 中文图文详
-
Python json解析模块loads/dumps中文e
-
Python random模块sample、randint、shu
-
Python re正则表达式操作指南
-
Python time模块 函数格式 时间操作
热点文章 HOT
- Python json解析模块loads/dumps中文encode教程
- Python re正则表达式操作指南
- Python threading多线程模块
- Python string模块 字符串函数方法操作教程
- Python robotparser 网络蜘蛛robots.txt搜索访问
- Python Cookie HTTP获取cookie并处理
- Python urllib模块 网络资源访问安装下载
- Python csv模块读写中文乱码等问题解决
- Python urllib2模块post/get 下载网络资源
- Python sys模块 argv path常用方法图文详解