Split string on whitespace in Python

siamii picture siamii · Nov 13, 2011 · Viewed 789k times · Source

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

Sven Marnach picture Sven Marnach · Nov 13, 2011

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

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