Intellipaat Back

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

I'm trying to find the most pythonic way to split a string like

"some words in a string"

into single words. string.split(' ') works ok but it returns a bunch of white space entries in the list. Of course, I could iterate the list and remove the white spaces but I was wondering if there was a better way?

1 Answer

0 votes
by (106k points)

You can use the regular expression to split string by an arbitrary number of white spaces:-

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

\s is short for any whitespace. So \s+ is contiguous whitespace.

To know more about this you can have a look at the following video tutorial:-

Related questions

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...