join()

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Mar 20 15:39:05 CET 2013


On Wed, 20 Mar 2013 07:05:04 -0700, franzferdinand wrote:

> I'm doing this "Write code that removes whitespace at the beginning and
> end of a string, and normalizes whitespace between words to be a single
> space character"
> 
> I can do it using split()
> 
>>>> raw = "      the     weather is     sunny                  today    "
>>>> raw.split()
> ['the', 'weather', 'is', 'sunny', 'today']
> 
> But how do I do it using join() ?? Thanks


First you split the string into a list of words, using the split method, 
just as you do above. Then you join the list of words back into a single 
string, using the joint method.

Hint:

"+".join(['the', 'quick', 'brown', 'fox'])

=> "the+quick+brown+fox"


Does that give you enough of a hint?



-- 
Steven



More information about the Python-list mailing list