Re: [Tutor] understanding join

2008-07-30 Thread John Fouhy
On 31/07/2008, Steve Poe [EMAIL PROTECTED] wrote:
 Hi tutor list,

  Just trying to add some clarity to the built-in function strings using
 join. The Python help
  screen says it returns a string which is a concatenation of strings in
 sequence. I am concatenating
  the string I am working on that maybe an issue of its own.
[...]
  but if string is 'abc'

  print string.join(string)
  aabcbabcc

Hi Steve,

First up, a quick comment: There is a string module in the standard
library, and it has a function called join().  So it's generally a
good idea not to use 'string' as a variable name, as it can confuse
people :-)

Now, to your question: your string is 'abc'.  It doesn't matter that
you're using a string to join itself; what you've written is identical
to:

 'abc'.join('abc')
'aabcbabcc'

Let's change the call slightly to make things more clear:

 'abc'.join('ABC')
'AabcBabcC'

Does that make the pattern more clear?

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


Re: [Tutor] understanding join

2008-07-30 Thread John Fouhy
On 31/07/2008, Steve Poe [EMAIL PROTECTED] wrote:
  Good point. I guess I am surprised a little that Python does not error
  on that you cannot assign a variable to a module name.  I know I need
  to learn proper coding techniques.

Well, that wouldn't really work because you don't know what other
modules people have.

  Okay, I can see the order of the join is the same as mine, but
  the join still seems to be out of sequence.  I am thinking it should be
  'abcABC' or 'ABCabc' not at the beginning, middle, and end of the
  original string. At least, I am trying to wrap my head around its
  usefulness.

Say I have a sequence seq and a string s, and I call s.join(seq).
Here's what it does:

s.join(seq) == seq[0] + s + seq[1] + s + seq[2] + s + ...  + seq[-2] +
s + seq[-1]

So if you call 'abc'.join('ABC'), you get:

   'ABC'[0] + 'abc' + 'ABC'[1] + 'abc' + 'ABC'[2]

which is:

   'A' + 'abc' + 'B' + 'abc' + 'C'

Hope this helps.

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


Re: [Tutor] understanding join

2008-07-30 Thread Kent Johnson
 On 31/07/2008, Steve Poe [EMAIL PROTECTED] wrote:
  Okay, I can see the order of the join is the same as mine, but
  the join still seems to be out of sequence.  I am thinking it should be
  'abcABC' or 'ABCabc' not at the beginning, middle, and end of the
  original string. At least, I am trying to wrap my head around its
  usefulness.

Does this look useful?

In [3]: people = [ 'Tom', 'Dick', 'Harry' ]

In [4]: ', '.join(people)
Out[4]: 'Tom, Dick, Harry'

Your confusion is in thinking about the string 'ABC' as a single
entity. For the purposes of join(), it is a sequence of three letters.
The argument to join() is a sequence of strings, not a single string.

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


Re: [Tutor] understanding join

2008-07-30 Thread Alan Gauld


Steve Poe [EMAIL PROTECTED] wrote


Your explanation is very help. It does make be wonder the usefulness
of join with strings. Do you have a practical example/situation?


Its not really intended for strings but it needs to work that way to
be consistent because strings are just another type of collection
in Python and we want to treat collections (or sequences)
as consistently as possible.

But there are times when you want to separate the characters
of a string out for display and inserting a space or a comma
using join is convenient. It would be extremely rare to use

mystring.joing(mystring)

It is usually

myseparator.join(mysequence)

HTH
--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



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