How can I take each word in a string and append it in a list?
本问题已经有最佳答案,请猛点这里访问。
我的字符串是:
1 | wish ="Happy New Year" |
输出:
1 | word_list = ['Happy','New','Year'] |
号
1 2 3 4 5 | >>> wish ="Happy New Year" >>> wordlist = wish.split() >>> wordlist ['Happy', 'New', 'Year'] >>> |
号
您可以看到字符串函数split的帮助。
1 2 3 4 5 6 7 8 9 10 11 12 13 | >>> 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 | "Happy New Year".split() |
这将生成您的数组。