join()

F

franzferdinand

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()
['the', 'weather', 'is', 'sunny', 'today']

But how do I do it using join() ?? Thanks
 
W

Wolfgang Maier

franzferdinand said:
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()
['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
 
S

Steven D'Aprano

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()
['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?
 
M

Mark Lawrence

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()
['the', 'weather', 'is', 'sunny', 'today']

But how do I do it using join() ?? Thanks

' '.join(raw.split())
 
T

Tim Chase

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
 
S

Steven D'Aprano

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top