用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']

结论:split()的时候,多个空格当成一个空格;split(' ')的时候,多个空格都要分割,每个空格分割出来空。

 

posted on 2018-12-05 20:50  少年与python  阅读(23931)  评论(1编辑  收藏  举报