On Mon, 30 Aug 2010 07:44:06 pm Roelof Wobben wrote: > Hello, > > > > For a exerise I made this one :" > > > > import string > > def extract_words(s): [...]
> But now I wonder if this can be done more easerly ? def extract_words(s): """ >>> extract_words('Now is the time! "Now", is the time?') ['now', 'is', 'the', 'time', 'now', 'is', 'the', 'time'] >>> extract_words('she tried to curtsy as she spoke--fancy') ['she', 'tried', 'to', 'curtsy', 'as', 'she', 'spoke', 'fancy'] """ s = s.lower() # Replace anything that isn't a letter with a space. chars = [c if c.isalpha() else " " for c in s] s = ''.join(chars) words = s.split() return words import doctest doctest.run_docstring_examples(extract_words, globals()) print extract_words("Hello, world! I ate 6 slices of cheese.") => prints: ['hello', 'world', 'i', 'ate', 'slices', 'of', 'cheese'] -- Steven D'Aprano _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor