如何将字符串中的每个单词并将其附加到列表中?

时间:2016-01-01 08:59:44

标签: python string list

我的字符串是:

wish = "Happy New Year"

输出:

word_list = ['Happy','New','Year']

2 个答案:

答案 0 :(得分:3)

>>> wish = "Happy New Year"
>>> wordlist = wish.split()
>>> wordlist
['Happy', 'New', 'Year']
>>>

你可以看到字符串函数split的帮助。

>>> help(''.split)
Help on built-in function split:

split(...)
    S.split([sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are removed
    from the result.

>>>

答案 1 :(得分:0)

请参阅字符串的split方法。

"Happy New Year".split()

这会产生你的数组。