Python Tutorial 20 : How to split every single words from a sentence in Python using split function

How to split every single words from a sentence in Python?

We can use the Split function

Suppose we want to split following paragraphs from the internet (www.traveller.com.au)

“Not everyone travels to get outside their comfort zone. Not everyone wants to challenge themselves by doing something new and intimidating every time they go on holidays, to travel to strange places and to do strange things. Some people just want to relax. And that’s fine.”

Source: http://www.traveller.com.au/the-11-countries-that-will-take-you-out-of-your-comfort-zone-h10xy0#ixzz5HXw6fMAh
Follow us: @TravellerAU on Twitter | TravellerAU on Facebook

 

Following is the script

Extract each words in a paragraph - Python

 

And the result:

Sentence 1 :’ [‘Not’, ‘everyone’, ‘travels’, ‘to’, ‘get’, ‘outside’, ‘their’, ‘comfort’, ‘zone’]
Sentence 2 :’ [‘Not’, ‘everyone’, ‘wants’, ‘to’, ‘challenge’, ‘themselves’, ‘by’, ‘doing’, ‘something’, ‘new’, ‘and’, ‘intimidating’, ‘every’, ‘time’, ‘they’, ‘go’, ‘on’, ‘holidays,’, ‘to’, ‘travel’, ‘to’, ‘strange’, ‘places’, ‘and’, ‘to’, ‘do’, ‘strange’, ‘things’]
Sentence 3 :’ [‘Some’, ‘people’, ‘just’, ‘want’, ‘to’, ‘relax’]
Sentence 4 :’ [‘And’, “that’s”, ‘fine’]
Sentence 5 :’ []

 

Following is the explanation to each steps

  1. Split each words into a temporary sentence by spitting the paragraph by a “.”
    [vtftable cols=”{0}0:fff2cc;{/}”]
    temp = paragraph.split(‘.’);nn;
    [/vtftable]
  2. Define a list called sentence. This list will be used to store all words for each sentence in future[vtftable cols=”{0}0:fff2cc;{/}”]
    sentence =[]
    [/vtftable]
  3. For each sentence (elements) in the temp list, split all words in to a list called “words” then append the word list into the sentence list[vtftable cols=”{0}0:fff2cc;{/}”]
    for s in temp:
    words = s.split()
    sentence.append(words)
    [/vtftable]After this step, the sentence list will have multiple sub-list elements. And each sub-list elements have one word in each element.
  4. Print each sentence[vtftable cols=”{0}0:fff2cc;{/}”]
    i = 1
    for line in sentence:
    print(“Sentence”,i,”:'” ,line)
    i = i + 1
    [/vtftable]

In[6]: sentence[1][1]
Out[6]: ‘everyone’

Leave a Comment