Python中split()和split(" ")的区别
2021/9/28 17:12:41
本文主要是介绍Python中split()和split(" ")的区别,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
结论:split()的时候,多个空格当成一个空格;split(' ')的时候,多个空格都要分割,每个空格分割出来空。用split(" ")测试:
![复制代码](https://common.cnblogs.com/images/copycode.gif)
1 s1 = "we are family"#中间一个空格 2 s2 = "we are family"#中间两个空格 3 s3 = "we are family"#中间三个空格 4 s4 = "we are family"#中间四个空格 5 6 s1 = s1.split(" ") 7 s2 = s2.split(" ") 8 s3 = s3.split(" ") 9 s4 = s4.split(" ") 10 11 print(s1)#['we', 'are', 'family'] 12 print(s2)#['we', '', 'are', '', 'family'] 13 print(s3)#['we', '', '', 'are', '', '', 'family'] 14 print(s4)#['we', '', '', '', 'are', '', '', '', 'family']
![复制代码](https://common.cnblogs.com/images/copycode.gif)
用split()测试:
![复制代码](https://common.cnblogs.com/images/copycode.gif)
1 s1 = "we are family"#中间一个空格 2 s2 = "we are family"#中间两个空格 3 s3 = "we are family"#中间三个空格 4 s4 = "we are family"#中间四个空格 5 6 s1 = s1.split() 7 s2 = s2.split() 8 s3 = s3.split() 9 s4 = s4.split() 10 11 print(s1)#['we', 'are', 'family'] 12 print(s2)#['we', 'are', 'family'] 13 print(s3)#['we', 'are', 'family'] 14 print(s4)#['we', 'are', 'family']
![复制代码](https://common.cnblogs.com/images/copycode.gif)
这篇关于Python中split()和split(" ")的区别的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python