Python教程

Python中split()和split(" ")的区别

本文主要是介绍Python中split()和split(" ")的区别,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
结论:split()的时候,多个空格当成一个空格;split(' ')的时候,多个空格都要分割,每个空格分割出来空。  

用split(" ")测试:

复制代码
 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']
复制代码

用split()测试:

复制代码
 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']
复制代码

 

这篇关于Python中split()和split(" ")的区别的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!