Intellipaat Back

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

I want my Python function to split a sentence (input) and store each word in a list. My current code splits the sentence but does not store the words as a list. How do I do that?

def split_line(text): 

     # split the text

     words = text.split()

     # for each word in the line:

     for word in words:

         # print the word

         print(words)

1 Answer

0 votes
by (106k points)

The following code splits the string in the text on any consecutive runs of whitespace:-

words = text.split()

By using the below-mentioned code you can split the string in the text on delimiter: ",":-

words = text.split(",")

And the words variable will be a list and it will contain the words from text split on the delimiter.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...