Dear John,

Thanks for your explanation. It is quite amazing, I had completely misunderstood what the join function was all about. An example in "Learning Python" which was something like "".join(L) , had led me to think it was a string concatenation function that extended the length of a string. Even when reading the manual my prejudice had interpreted the following as still being just that. My subconscious had skipped over the second sentence.

"join(seq) Return a string which is the concatenation of the strings in the sequence seq. The separator between elements is the string providing this method. "
 
Fortunately my new Python instinct suggested what I was doing was not quite right, and thank you for correcting my misunderstanding.

This Tutor service is an invaluable help for us lonely new to Python programmers.

Thanks,

Roy


PS Despite this glitch I would still recommend Learning Python.


Message: 5
Date: Mon, 14 Nov 2005 17:11:16 +1300
From: John Fouhy <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Converting a List/Tuple to delimited string
To: Tutor <tutor@python.org>
Message-ID: <[EMAIL PROTECTED]>
Content-Type: text/plain; charset=ISO-8859-1
On 14/11/05, Roy Bleasdale <[EMAIL PROTECTED]> wrote:
> I have a list of strings and wanted to output them as a single delimited
> string.
>
> Eg
>
> ('ab','cd','ef') becomes "ab:cd:ef"
You almost had it ...
What about:
>>> lst = ['ab', 'cd', 'ef']
>>> ':'.join(lst)
'ab:cd:ef'
In general, foo.join(lst) (where foo is a string) is equivalent to:
def join(foo, lst):
if not lst:
return ''
s = lst[0]
for item in lst[1:]:
s += foo + item
return s
--
John.

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to