The difference between split () and Split (') in Python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.