Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Basic utility functions
Basic manipulation functions
Replacing and splitting
11. The split() function
Summary

Instruction

You did well in the previous exercise! There is actually another powerful function that can help solve similar problems even more easily. The function is named split(), and it is used to split a string into multiple parts: lines, sentences, words, etc. Take a look:

nursery_rhyme = 'Doctor Foster went to Gloucester,/In a shower of rain;/He stepped in a puddle,/Right up to his middle,/And never went there again.'
verses = nursery_rhyme.split('/')

The string.split(separator) function takes a separator as its argument and uses it to split the string into parts. The function produces a list of substrings as a result. In the example above, verses will contain the following:

['Doctor Foster went to Gloucester,',
  'In a shower of rain;',
  'He stepped in a puddle,',
  'Right up to his middle,',
  'And never went there again.']

Once you have your string split into pieces, you can process them one by one in a for loop, like this:

for verse in verses:
  print(verse + ' [' + str(len(verse)) + ' characters]')

Exercise

Write a Python program that accepts a paragraph from the user:

Provide a paragraph of text: 

The program should return the longest word in the paragraph along with its length:

The longest word is {x} with a length of {y}.

If there is more than one word with the most letters, return the first such word.

Note: Punctuation may be a problem when splitting the paragraph into words. Assume that only two types of punctuation marks are used: commas and full stops, both of which are always followed by a space. Get rid of those marks, and only then split the paragraph.

Stuck? Here's a hint!

To get rid of commas and full stops, use:

paragraph.replace(',','').replace('.','')