Why does split() return more elements than split(“ ”) on same string?

前端 未结 5 1686
闹比i
闹比i 2021-01-15 03:52

I am using split() and split(\" \") on the same string. But why is split(\" \") returning less number of elements than split()

5条回答
  •  不思量自难忘°
    2021-01-15 04:21

    In Python, the split function splits on a specific string if specified, otherwise on spaces (and then you can access the result list by index as usual):

    s = "Hello world! How are you?"
    s.split() 
    Out[9]:['Hello', 'world!', 'How', 'are', 'you?']
    s.split("!")
    Out[10]: ['Hello world', ' How are you?'] 
    s.split("!")[0] 
    Out[11]: 'Hello world' 
    

提交回复
热议问题