Python中按空格拆分字符串

107 浏览
0 Comments

Python中按空格拆分字符串

这个问题已经有答案了

如何将一个字符串分割为单词列表?

我正在寻找Python语言中等价的

String str = "many   fancy word \nhello    \thi";
String whiteSpaceRegex = "\\s";
String[] words = str.split(whiteSpaceRegex);
["many", "fancy", "word", "hello", "hi"]

admin 更改状态以发布 2023年5月23日
0
0 Comments
import re
s = "many   fancy word \nhello    \thi"
re.split('\s+', s)

,即粗体字123。

0
0 Comments

str.split()方法不带参数会按照空格进行分割:

>>> "many   fancy word \nhello    \thi".split()
['many', 'fancy', 'word', 'hello', 'hi']

0