Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)
edited by

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 (106k points)
selected by
Best answer

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()


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)


Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer

Browse Categories