Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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"]

closed

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

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

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
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...