0 votes
1 view
in Python by (47.8k 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 (107k 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
asked Oct 14, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
1 answer
asked Sep 27, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...