How to split a string into a list?

后端 未结 9 2458
执念已碎
执念已碎 2020-11-21 04:32

I want my Python function to split a sentence (input) and store each word in a list. My current code splits the sentence, but does not store the words as a list. How do I do

9条回答
  •  醉话见心
    2020-11-21 05:25

    str.split()

    Return a list of the words in the string, using sep as the delimiter ... If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

    >>> line="a sentence with a few words"
    >>> line.split()
    ['a', 'sentence', 'with', 'a', 'few', 'words']
    >>> 
    

提交回复
热议问题