Split string on whitespace in Python [duplicate]

ghz 1years ago ⋅ 10041 views

Question

This question already has answers here :

[How do I split a string into a list of words?](/questions/743806/how-do-i- split-a-string-into-a-list-of-words) (10 answers)

Closed 6 years ago.

I'm looking for the Python equivalent of

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

["many", "fancy", "word", "hello", "hi"]

Answer

The str.split() method without an argument splits on whitespace:

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