Join strings - very simple Q.

Shane Geiger sgeiger at ncee.net
Sat Mar 24 14:02:23 CET 2007


import string

def test_join(l):
    print "Joining with commas:                     ",  string.join(l,',')
    print "Joining with empty string:               ",  string.join(l,'')
    print "Joining same way, using another syntax:  ",  ''.join(l)
    print "Joining with the letter X:               ",  'X'.join(l)
    print "Joining with <->                         ",  '<->'.join(l)

l = ['a','b','c']

test_join(l)


"""
Example output:

Joining with commas:                      a,b,c
Joining with empty string:                abc
Joining same way, using another syntax:   abc
Joining with the letter X:                aXbXc
Joining with <->                          a<->b<->c

"""






Dustan wrote:
> On Mar 24, 5:59 am, "Dustan" <DustanGro... at gmail.com> wrote:
>   
>> On Mar 23, 1:30 pm, Paulo da Silva <psdasil... at esotericaX.ptX> wrote:
>>
>>     
>>> Mike Kent escreveu:
>>> ...
>>>       
>>>> New way:
>>>> l=['a','b','c']
>>>> jl=','.join(l)
>>>>         
>>> I thank you all.
>>>       
>>> Almost there ...
>>> I tried "".join(l,',') but no success ... :-(
>>>       
>>> Paulo
>>>       
>> Perhaps you're doing it wrong, despite having an example right in
>> front of you?
>>
>> Side by side comparison:
>> jl=string.join(l,',')
>> jl=','.join(l)
>>
>> The sequence is passed as an argument to the join method, and the
>> delimiter is the string whose method is being called.
>>     
>
> To further demonstrate (because I got a weird email that seemed to
> think that my code didn't work):
>
>   
>>>> import string
>>>> l = ['a','b','c']
>>>> string.join(l,',')
>>>>         
> 'a,b,c'
>   
>>>> ','.join(l)
>>>>         
> 'a,b,c'
>
>   

-- 
Shane Geiger
IT Director
National Council on Economic Education
sgeiger at ncee.net  |  402-438-8958  |  http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sgeiger.vcf
Type: text/x-vcard
Size: 310 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20070324/8aaa15c9/attachment.vcf>


More information about the Python-list mailing list