Summary: Split (), when multiple spaces as a space, split ("), when multiple spaces are divided, will be split out empty. Example 1:
New Ket: New Ket has recently come to a new employee fish, every morning always holding an English magazine, write some sentences in the book. Colleague Cat was interested in what Fish wrote, one day he borrowed it from fish, but couldn't read it. For example, "student. A am I ". Later realized that this guy turned the sentence in the order of words, the correct sentence should be "I am a student." Cat Flip One by one the order of these words is not good, can you help him?
This code can be passed:
s='Student a AM I's=' ' if notS:Print(s) s=s.split (' ') Result=[] forIinchRange (len (s) -1,-1,-1): Result.append (S[i])ifI#The last position does not need to add a spaceResult.append (' ') M="'. Join (Result) length=(Len (m))Print(m)Print(length)
Output:
1
If you remove the space in Split ("), you cannot handle the case where the input is empty:
s='Student a AM I's=' ' if notS:Print(s) s=s.split () result=[] forIinchRange (len (s) -1,-1,-1): Result.append (S[i])ifI#The last position does not need to add a spaceResult.append (' ') M="'. Join (Result) length=(Len (m))Print(m)Print(length)
Output:
0
Example 2:
Test with split () to see:
s0='We are students'#a spaces1='We are students'#Two spacesS2='We are students'#Three spacess3='We are students'#Four spacesS0=S0.split ()Print(S0)Print(Len (s0)) S1=S1.split ()Print(S1)Print(len (S1)) S2=S2.split ()Print(S2)Print(len (S2)) S3=S3.split ()Print(S3)Print(Len (S3))
The output is:
['We',' is','Students']3['We',' is','Students']3['We',' is','Students']3['We',' is','Students']3
Test with Split (') to see:
s0='We are students'#a spaces1='We are students'#Two spacesS2='We are students'#Three spacess3='We are students'#Four spacesS0=s0.split (' ')Print(S0)Print(Len (s0)) S1=s1.split (' ')Print(S1)Print(len (S1)) S2=s2.split (' ')Print(S2)Print(len (S2)) S3=s3.split (' ')Print(S3)Print(Len (S3))
Output:
['We',' is','Students']3['We',' is',"','Students']4['We',' is',"',"','Students']5['We',' is',"',"',"','Students']6
Summary: Split (), when multiple spaces as a space, split ("), when multiple spaces are divided, will be split out empty.
The difference between split () and Split (') in Python