join()

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Wed Mar 20 15:34:55 CET 2013


franzferdinand <melo.dumoulin <at> hotmail.com> writes:

> 
> 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
> 

I guess you mean: how can I re-generate a string from my list of sub-strings,
where each element is separated by only a single space.

' '.join(raw.split())

join is a string method that can take a list of strings to join as its argument.
Best,
Wolfgang







More information about the Python-list mailing list