0 votes
1 view
in Python by (14.2k points)

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"]

1 Answer

0 votes
by (30.4k points)

For splitting the string on whitespace in Python you can use the str.split() function without any argument it will split strings on whitespace below is an example that shows how to use str.split():-

>>>"Hello programmers enjoy the programming world".split()

image

There is another method which uses a regular expression to splits the strings on to whitespace below is the code that illustrates how we do it:-

import re

s = "Hello programmers enjoy the programming world"

re.split('\s+', s)

image

...