Re: [Tutor] understanding join
"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
Re: [Tutor] understanding join
"Steve Poe" <[EMAIL PROTECTED]> wrote In [3]: people = [ 'Tom', 'Dick', 'Harry' ] Okay, now let's join people to people and what do we get? An error, join only works on a single string. It joins the elements of a sequence of strings into a single string using the 'owning' string. In some ways, from an OOP point of view the method is counterintuitive. It should really be a method of a sequejnce taking a string as argument: [1,2,3].join('/') makes more sense to me than '/'.join(['1','2','3']) But the second is the correct form. I found the string module function more readable import string string.join(['1','2','3'], '/') Not least because you could omit the second argument and get a default space. Making join a member of the sequence would have allowed the default behaviour to continue. But I assume there were subtle snags with that scheme. Just my personal opinion... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding join
On 31/07/2008, 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? Kent's example is common: you might have a list of strings that you want to display to the user, so you call ', '.join() on the list. Calling ''.join() is the standard way in Python to concatenate a bunch of strings. If you've got some two-dimensional data structure that you want to convert to CSV, you could use the csv module, but if your data is simple, it might be easier to just do: '\n'.join(','.join(row) for row in data) I guess it depends what kind of programming you're doing, but in my experience, .join() is definitely a useful function. -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding join
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. John, Your explanation is very help. It does make be wonder the usefulness of join with strings. Do you have a practical example/situation? Steve ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding join
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 Kent, Your explanation about my confusion is right on target. Thank you! Okay, now let's join people to people and what do we get? Steve ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] understanding join
> 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
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
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