[Tutor] understanding join

John Fouhy john at fouhy.net
Wed Jul 30 23:54:06 CEST 2008


On 31/07/2008, Steve Poe <steve.poe at gmail.com> 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.


More information about the Tutor mailing list