Python | Split a sentence into list of words
Given a Sentence, write a Python program to convert the given sentence into a list of words.
Input : 'Hello World' Output : ['Hello', 'world']
Method 1: Split a sentence into a list using split()
The simplest approach provided by Python to convert the given list of Sentences into words with separate indices is to use split() method. This method split a string into a list where each word is a list item. We have alternative ways to use this function in order to achieve the required output.
['Geeks', 'For', 'geeks']
Method 2: Split a sentence into a list using for loop
We can also use a Python for loop to split the first element. This method is also beneficial if we have more than one element.
['Geeksforgeeks', 'is', 'a', 'portal', 'for', 'geeks']
Method 3: Split a sentence into a list using join()
We can split the given list and then join using join() function. We can also use this when you have a list of strings or a single string inside a list.
['Hello', 'Geeks', 'for', 'geeks']
Method 4: Split a sentence into a list using nltk
For our particular issue, the nltk library’s word tokenize() method can be used. This function divides a string into several substrings by taking a string as an input parameter.
['This', 'is', 'geeksforgeeks']