Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41592 > unrolled thread

join()

Started byfranzferdinand <melo.dumoulin@hotmail.com>
First post2013-03-20 07:05 -0700
Last post2013-03-20 07:46 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  join() franzferdinand <melo.dumoulin@hotmail.com> - 2013-03-20 07:05 -0700
    Re: join() Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-20 14:34 +0000
    Re: join() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-20 14:39 +0000
      Re: join() Tim Chase <python.list@tim.thechases.com> - 2013-03-20 10:54 -0500
        Re: join() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-20 18:44 +0000
    Re: join() Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-20 14:44 +0000
      Re: join() franzferdinand <melo.dumoulin@hotmail.com> - 2013-03-20 07:46 -0700

#41592 — join()

Fromfranzferdinand <melo.dumoulin@hotmail.com>
Date2013-03-20 07:05 -0700
Subjectjoin()
Message-ID<6110d82b-fce9-4146-8fcc-3276334a8f5f@14g2000vbr.googlegroups.com>
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

[toc] | [next] | [standalone]


#41598

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2013-03-20 14:34 +0000
Message-ID<mailman.3565.1363790118.2939.python-list@python.org>
In reply to#41592
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



[toc] | [prev] | [next] | [standalone]


#41599

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-20 14:39 +0000
Message-ID<5149ca09$0$30001$c3e8da3$5496439d@news.astraweb.com>
In reply to#41592
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

[toc] | [prev] | [next] | [standalone]


#41606

FromTim Chase <python.list@tim.thechases.com>
Date2013-03-20 10:54 -0500
Message-ID<mailman.3568.1363794767.2939.python-list@python.org>
In reply to#41599
On 2013-03-20 14:39, Steven D'Aprano wrote:
> Then you join the list of words back
> into a single string, using the joint method.

Sometimes when I see people make spelling errors, I wonder what they
were smoking.  Sometimes, it's more obvious... ;-)

-tkc

[toc] | [prev] | [next] | [standalone]


#41615

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-20 18:44 +0000
Message-ID<514a0399$0$30001$c3e8da3$5496439d@news.astraweb.com>
In reply to#41606
On Wed, 20 Mar 2013 10:54:22 -0500, Tim Chase wrote:

> On 2013-03-20 14:39, Steven D'Aprano wrote:
>> Then you join the list of words back
>> into a single string, using the joint method.
> 
> Sometimes when I see people make spelling errors, I wonder what they
> were smoking.  Sometimes, it's more obvious... ;-)


Oh please, I think it's quite cruel to make fun of me for making a 
perfectly innocent slip of the tongue here, when it's quite obvious I 
meant my mother.


Sometimes-a-cigar-is-just-a-phallic-symbol-ly y'rs,


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#41600

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-03-20 14:44 +0000
Message-ID<mailman.3566.1363790630.2939.python-list@python.org>
In reply to#41592
On 20/03/2013 14:05, 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
>

' '.join(raw.split())

-- 
Cheers.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#41601

Fromfranzferdinand <melo.dumoulin@hotmail.com>
Date2013-03-20 07:46 -0700
Message-ID<4c009df9-c047-4e05-85d3-fefeaebac978@z4g2000vbz.googlegroups.com>
In reply to#41600
Yeah it works. Thank you everybody!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web