[FRPythoneers] Question about string.join method

jafo at tummy.com jafo at tummy.com
Mon Apr 30 15:18:27 MDT 2001

On Mon, Apr 30, 2001 at 11:30:39AM -0600, J. Wayde Allen wrote:
>           self.latitude = string.join(field[2], field[3])

You're misunderstanding string.join.  It's meant to be the reverse of

   >>> l1 = string.split('This is a test of string.split')
   >>> l2 = string.join(l1)
   >>> print l1
   ['This', 'is', 'a', 'test', 'of', 'string.split']
   >>> print l2
   This is a test of string.split

I think this kind of explains it:

   >>> print string.join.__doc__
   join(list [,sep]) -> string
   joinfields(list [,sep]) -> string

   Return a string composed of the words in list, with
   intervening occurences of sep.  Sep defaults to a single

   (join and joinfields are synonymous)

In this case, "list" as the argument is kind of mis-leading.  It actually
takes a sequence.  A list is a sequence, but so is a string.  So, if you
pass it a string as the first argument, it's going to not do what you
expected.  ;-/

Your original "field[2] + field[3]" was correct.  It can also be written

   res = '%s%s' % ( field[2], field[3] )
   res = string.join(( field[2], field[3] ), '')

 "8 out of 10 Starships that took the Folger's challenge didn't know they
 weren't using Dilithium Crystals."  -- Sean and Anne being silly one night.
Sean Reifschneider, Inimitably Superfluous <jafo at tummy.com>
tummy.com - Linux Consulting since 1995. Qmail, KRUD, Firewalls, Python

More information about the FRPythoneers mailing list