Python3 CookBook中如何处理字符串和文本操作?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1631个文字,预计阅读时间需要7分钟。
欢迎关注我的微信公众号+AlwaysBeta,更多精彩内容等你来!
以下测试代码全部基于Python3编写。
字符串操作在程序中的出现频率相当高,包括分割、替换、拼接等。本文介绍五个字符串操作技巧:
1. 分割字符串pythons=hello,worldlist_s=s.split(',')print(list_s) # 输出:['hello', 'world']
2. 替换字符串pythons=hello,worlds=s.replace('world', 'Python')print(s) # 输出:hello,Python
3. 拼接字符串pythons1=hellos2=worlds=s1 + s2print(s) # 输出:helloworld
4. 去除字符串首尾空格pythons= hello world s=s.strip()print(s) # 输出:hello world
5. 检查字符串是否以特定子串开头或结尾pythons=hello worldprint(s.startswith('hello')) # 输出:Trueprint(s.endswith('world')) # 输出:True
欢迎关注我的微信公众号 AlwaysBeta,更多精彩内容等你来。
以下测试代码全部基于 Python3。
字符串操作在程序中的出现频率相当高,包括分割,替换,拼接等等,这篇文章介绍五个最常遇到的问题,希望给你带来一些思考。
1、使用多个界定符分割字符串
分割字符串属于字符串最基本的操作了,直接用 split() 即可。
本文共计1631个文字,预计阅读时间需要7分钟。
欢迎关注我的微信公众号+AlwaysBeta,更多精彩内容等你来!
以下测试代码全部基于Python3编写。
字符串操作在程序中的出现频率相当高,包括分割、替换、拼接等。本文介绍五个字符串操作技巧:
1. 分割字符串pythons=hello,worldlist_s=s.split(',')print(list_s) # 输出:['hello', 'world']
2. 替换字符串pythons=hello,worlds=s.replace('world', 'Python')print(s) # 输出:hello,Python
3. 拼接字符串pythons1=hellos2=worlds=s1 + s2print(s) # 输出:helloworld
4. 去除字符串首尾空格pythons= hello world s=s.strip()print(s) # 输出:hello world
5. 检查字符串是否以特定子串开头或结尾pythons=hello worldprint(s.startswith('hello')) # 输出:Trueprint(s.endswith('world')) # 输出:True
欢迎关注我的微信公众号 AlwaysBeta,更多精彩内容等你来。
以下测试代码全部基于 Python3。
字符串操作在程序中的出现频率相当高,包括分割,替换,拼接等等,这篇文章介绍五个最常遇到的问题,希望给你带来一些思考。
1、使用多个界定符分割字符串
分割字符串属于字符串最基本的操作了,直接用 split() 即可。

