By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Join strings - very simple Q.

P: n/a
Hi!

I was told in this NG that string is obsolet. I should use
str methods.

So, how do I join a list of strings delimited by a given
char, let's say ','?

Old way:

l=['a','b','c']
jl=string.join(l,',')

New way?

Thanks
Paulo
Mar 23 '07 #1
Share this Question
Share on Google+
27 Replies


P: n/a
Paulo da Silva <ps********@esotericaX.ptXwrites:
Hi!

I was told in this NG that string is obsolet. I should use
str methods.

So, how do I join a list of strings delimited by a given
char, let's say ','?

Old way:

l=['a','b','c']
jl=string.join(l,',')

New way?
Dunno if it's the "new way", but you can do: ''.join(l)

Mar 23 '07 #2

P: n/a
On Mar 23, 2:37 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
Hi!

I was told in this NG that string is obsolet. I should use
str methods.

So, how do I join a list of strings delimited by a given
char, let's say ','?

Old way:

l=['a','b','c']
jl=string.join(l,',')

New way?

Thanks
Paulo
New way:
l=['a','b','c']
jl=','.join(l)

Mar 23 '07 #3

P: n/a
Paul Rudin <pa********@ntlworld.comwrites:
Paulo da Silva <ps********@esotericaX.ptXwrites:
>Hi!

I was told in this NG that string is obsolet. I should use
str methods.

So, how do I join a list of strings delimited by a given
char, let's say ','?

Old way:

l=['a','b','c']
jl=string.join(l,',')

New way?

Dunno if it's the "new way", but you can do: ''.join(l)
Err, sorry - missed the comma out - it should be: ','.join(l)
Mar 23 '07 #4

P: n/a
I was told in this NG that string is obsolet. I should use
str methods.

So, how do I join a list of strings delimited by a given
char, let's say ','?

Old way:

l=['a','b','c']
jl=string.join(l,',')

New way?

Dunno if it's the "new way", but you can do: ''.join(l)
The OP wants the strings to be comma delimited:

jl=','.join(l)
Mar 23 '07 #5

P: n/a
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
Mar 23 '07 #6

P: n/a
On Mar 24, 5:37 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
Hi!

I was told in this NG that string is obsolet. I should use
str methods.

So, how do I join a list of strings delimited by a given
char, let's say ','?

Old way:

l=['a','b','c']
jl=string.join(l,',')

New way?
Self-help #1: reading the documentation: http://www.python.org/doc/2.0/lib/string-methods.html
?
Change "0" to "5" to get the latest released version -- which hasn't
changed the description of the join method AFAICT.

Self-help #2: help() at the interactive prompt:

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
| >>help("".join)
Help on built-in function join:

join(...)
S.join(sequence) -string

Return a string which is the concatenation of the strings in the
sequence. The separator between elements is S.

| >>>

OK, I'll bite: This was "new" in late 2000 when Python 2.0 was
released. Where have you been in the last ~6.5 years?

HTH,
John

Mar 23 '07 #7

P: n/a
On Fri, 23 Mar 2007 13:15:29 -0700, John Machin wrote:
OK, I'll bite: This was "new" in late 2000 when Python 2.0 was
released. Where have you been in the last ~6.5 years?
Western civilization is 6,000 years old. Anything after 1850 is "new".

*wink*

--
Steven.

Mar 24 '07 #8

P: n/a
On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
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.

Mar 24 '07 #9

P: n/a
On Mar 24, 5:59 am, "Dustan" <DustanGro...@gmail.comwrote:
On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
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'

Mar 24 '07 #10

P: n/a
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...@gmail.comwrote:
>On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:

>>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
sg*****@ncee.net | 402-438-8958 | http://www.ncee.net

Leading the Campaign for Economic and Financial Literacy
Mar 24 '07 #11

P: n/a
"Dustan" <Du**********@gmail.comwrote:
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.
In case you are feeling that the ','.join(l) looks a bit jarring, be aware
that there are alternative ways to write it. You can call the method on the
class rather than the instance:

jl = str.join(',', l)
jl = unicode.join(u'\u00d7', 'l')

has the advantage of looking almost the same as the string function except
that the order of the arguments is reversed, the catch is you need to know
the type of the separator in advance. If you have a lot of calls with the
same separator then you can also save the method in an appropriately named
variable:

commaseparated = ','.join
...
jl = commaseparated(l)

Mar 24 '07 #12

P: n/a
Dustan escreveu:
On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
>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?
Some misunderstanding here ...
The post was just to thank. I was refering to what I tried
before posting the question here.

Regards
Paulo
Mar 24 '07 #13

P: n/a
John Machin escreveu:
...
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
| >>help("".join)
Help on built-in function join:

join(...)
S.join(sequence) -string

Return a string which is the concatenation of the strings in the
sequence. The separator between elements is S.

| >>>

OK, I'll bite: This was "new" in late 2000 when Python 2.0 was
released. Where have you been in the last ~6.5 years?
In a response to one of my posts I was told 'string' is
obsolet. 'string' was enough for me, but if it is obsolet
then there should be a *new* way, isn't it? The *new*
way I was told to use is "str methods".
I tried that *new* way to do *old* 'string' job. Voila
the reason of my so pertinent question.

I am deeply sorry to have disturbed you in your python's heaven.

Next time I'll read all books available, all texts including
the python formal description before
posting, or perhaps it would be better and more simple if you
just ignore my posts.

Bye.
Paulo

Mar 24 '07 #14

P: n/a
Steven D'Aprano escreveu:
On Fri, 23 Mar 2007 13:15:29 -0700, John Machin wrote:
.....
>
Western civilization is 6,000 years old.
After reading that post I wouldn't talk about
civilization, western or any other :-)

Regards.
Paulo
Mar 24 '07 #15

P: n/a
On Mar 24, 7:16 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
Dustan escreveu:
On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
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?

Some misunderstanding here ...
The post was just to thank. I was refering to what I tried
before posting the question here.
Ah. Sorry; I didn't realize that.
Regards
Paulo

Mar 24 '07 #16

P: n/a
On Mar 24, 8:30 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
In case you are feeling that the ','.join(l) looks a bit jarring, be aware
that there are alternative ways to write it. You can call the method on the
class rather than the instance:

jl = str.join(',', l)
jl = unicode.join(u'\u00d7', 'l')

... the catch is you need to know
the type of the separator in advance.
When I try the latter example, I get an error:

lst = ["hello", "world"]
print unicode.join(u"\u00d7", lst)

Traceback (most recent call last):
File "test1.py", line 2, in ?
print unicode.join(u"\u00d7", lst)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd7' in
position 5: ordinal not in range(128)

Mar 24 '07 #17

P: n/a
7stud schrieb:
On Mar 24, 8:30 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
>In case you are feeling that the ','.join(l) looks a bit jarring, be aware
that there are alternative ways to write it. You can call the method on the
class rather than the instance:

jl = str.join(',', l)
jl = unicode.join(u'\u00d7', 'l')

... the catch is you need to know
the type of the separator in advance.

When I try the latter example, I get an error:

lst = ["hello", "world"]
print unicode.join(u"\u00d7", lst)

Traceback (most recent call last):
File "test1.py", line 2, in ?
print unicode.join(u"\u00d7", lst)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd7' in
position 5: ordinal not in range(128)
You are mixing unicode with bytestrings here. make "hello" u"hello",
same for "world".

Diez
Mar 24 '07 #18

P: n/a
"Diez B. Roggisch" <de***@nospam.web.dewrote:
7stud schrieb:
>When I try the latter example, I get an error:

lst = ["hello", "world"]
print unicode.join(u"\u00d7", lst)

Traceback (most recent call last):
File "test1.py", line 2, in ?
print unicode.join(u"\u00d7", lst)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd7' in
position 5: ordinal not in range(128)

You are mixing unicode with bytestrings here. make "hello" u"hello",
same for "world".
Sorry Diez, wrong answer. A unicode separator will cause all the strings
being joined to be decoded using the default encoding (which could cause
problems with non-ascii characters in the decoded strings), but the problem
here is with encoding, not decoding.

7stud: the problem isn't the join, it is printing the string on your
terminal which is the problem. Try just:

print u"\u00d7"

and you'll get the same problem. Or:

lst = ["hello", "world"]
joined = unicode.join(u"\u00d7", lst)

will work, but you'll still have problems printing the result.

If you try it using a Python interpreter with an appropriate output
encoding it will work (idle can handle it on my system, but the DOS prompt
with its default codepage cannot).
Mar 24 '07 #19

P: n/a
"7stud" <bb**********@yahoo.comwrote:
On Mar 24, 8:30 am, Duncan Booth <duncan.bo...@invalid.invalid>
wrote:
>In case you are feeling that the ','.join(l) looks a bit
jarring, be aware that there are alternative ways to write it.
You can call the method on the class rather than the instance:

jl = str.join(',', l)
jl = unicode.join(u'\u00d7', 'l')

... the catch is you need to know
the type of the separator in advance.

When I try the latter example, I get an error:

lst = ["hello", "world"]
print unicode.join(u"\u00d7", lst)

Traceback (most recent call last):
File "test1.py", line 2, in ?
print unicode.join(u"\u00d7", lst)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd7'
in position 5: ordinal not in range(128)
Your terminal is likely the problem. Get rid of the print:

q=unicode.join(u'\u00d7',['hello','world'])

and you will probably get rid of the exception.

(so I guess the issue is the display, not the logic)
max

Mar 24 '07 #20

P: n/a
On Mar 25, 12:32 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
John Machin escreveu:
..
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
| >>help("".join)
Help on built-in function join:
join(...)
S.join(sequence) -string
Return a string which is the concatenation of the strings in the
sequence. The separator between elements is S.
| >>>
OK, I'll bite: This was "new" in late 2000 when Python 2.0 was
released. Where have you been in the last ~6.5 years?

In a response to one of my posts I was told 'string' is
obsolet. 'string' was enough for me, but if it is obsolet
then there should be a *new* way, isn't it? The *new*
way I was told to use is "str methods".
I tried that *new* way to do *old* 'string' job.
So much was obvious from your post.
Voila
the reason of my so pertinent question.
What was not obvious was (1) if you have been using Python for a
while, how you managed to be unaware of str methods (2) if you are a
newbie, how you managed to find out about the string module but not
find out about str methods [e.g. it's easy to miss the one line in the
"official" Python tutorial that refers to them] (3) why you were not
using obvious(?) methods to find out for yourself -- much faster than
posing a question on the newsgroup, and you don't have to face the
possibility of an impertinent response :-)
I am deeply sorry to have disturbed you in your python's heaven.
I wasn't disturbed at all (you would have to try much harder), only
mildly curious.
>
Next time I'll read all books available, all texts including
the python formal description before
posting,
A rather strange way of finding an answer to a simple question. A
focussed search (or an index!) is more appropriate. Why would you
expect the python formal description to have details on the syntax of
individual class methods? Do you always do a serial scan of all tables
for any database query? In addition to approaches I mentioned earlier,
you could try:

(1) going to the docs page on the Python website (http://
www.python.org/doc/), click on the "Search" link about one-third down
the page, and search for "join". You would get 6 results, one of which
is "join() (string method)"

(2) If you are using Windows, use the index tab in the Python-supplied
docs gadget: type in "join" and get the same 6 results. I believe this
facility is available (through a 3rd party) on Linux. [Having the docs
on your PC or USB storage device is very useful if you are working in
an environment where access to the Internet is restricted].

(3) Use Google groups, go to comp.lang.python, type "join string" into
the search box, and click on "Search this group". First result is the
current thread; most (or maybe all) of the next six or so will give
you the syntax your were looking for.

(4) Using Google or any other reasonable web searcher, search for the
3 words: Python join string. Any one of the first few hits gives the
answer you were seeking.

HTH,
John

Mar 24 '07 #21

P: n/a

"Steven D'Aprano" <st***@REMOVE.THIS.cyb...urce.com.auwrote:

On Fri, 23 Mar 2007 13:15:29 -0700, John Machin wrote:
OK, I'll bite: This was "new" in late 2000 when Python 2.0 was
released. Where have you been in the last ~6.5 years?

Western civilization is 6,000 years old. Anything after 1850 is "new".

*wink*
another nibble:

What happened in 1850 to make it the demarcation line?

I would have thought that the atom bomb round about 1945/6 would
be a crisper marker..

- Hendrik

Mar 25 '07 #22

P: n/a
Hendrik van Rooyen <ma**@microcorp.co.zawrote:
"Steven D'Aprano" <st***@REMOVE.THIS.cyb...urce.com.auwrote:

On Fri, 23 Mar 2007 13:15:29 -0700, John Machin wrote:
OK, I'll bite: This was "new" in late 2000 when Python 2.0 was
released. Where have you been in the last ~6.5 years?
Western civilization is 6,000 years old. Anything after 1850 is "new".

*wink*

another nibble:

What happened in 1850 to make it the demarcation line?
California became a US State. A pretty good demarcation point, I'd say!
Alex
Mar 25 '07 #23

P: n/a

On Mar 25, 2007, at 6:33 AM, Dennis Lee Bieber wrote:
On Sat, 24 Mar 2007 10:53:20 +0200, "Hendrik van Rooyen"
<ma**@microcorp.co.zadeclaimed the following in comp.lang.python:
>>
What happened in 1850 to make it the demarcation line?
Well... The Modified Julian Date zero is 17 November 1858
American Express is founded by Henry Wells & William Fargo on 18
March 1850?

Mar 25 '07 #24

P: n/a
Dustan escreveu:
On Mar 24, 7:16 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
>Dustan escreveu:
>>On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
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?
Some misunderstanding here ...
The post was just to thank. I was refering to what I tried
before posting the question here.

Ah. Sorry; I didn't realize that.
My fault. The post could well be read the way you did.
English is not my 1st language anyway.
Regards.
Paulo
Mar 25 '07 #25

P: n/a
John Machin escreveu:
On Mar 25, 12:32 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
>John Machin escreveu:
...
What was not obvious was (1) if you have been using Python for a
while, how you managed to be unaware of str methods (2) if you are a
newbie, how you managed to find out about the string module but not
find out about str method
>
This is a nice argument for perhaps a politician ...
I am not exactly a newbie if you talk about the time I have
been using Python but I have used it very few times and
sparsely in time. I have always used 'string' to do the
strings stuff. When I need a method, I just search the python
library manual index. Then I have a page with all string methods
available. This is why I knew 'string' and didn't know str.
>
[e.g. it's easy to miss the one line in the
"official" Python tutorial that refers to them] (3) why you were not
using obvious(?) methods to find out for yourself -- much faster than
posing a question on the newsgroup, and you don't have to face the
possibility of an impertinent response :-)
That's the kind of answer I hate. Of course anyone could find an
answer to any questions without posting them to this NG.
Google, Python inumerous manuals, books, etc. So, why the NG existence?
The reason we post here is because sometimes it is easier,
or we wait for any further discussion on the subject or just
because we want it.
There are lots of people in this NG with different levels of
knowledge. If you feel that it is a question that deserves the
honour of your response, just do it. Write it on the stones and
send them down to the common people :-) . If not, just close your eyes
and let others respond. If the question does not deserve any response,
then the people will judge and will leave it unanswered.
....
....
you could try:

(1) going to the docs page on the Python website (http://
www.python.org/doc/), click on the "Search" link about one-third down
the page, and search for "join". You would get 6 results, one of which
is "join() (string method)"
This, together with the answer other people here gave, is helpful.
When I posted I thought the subjec was new, do you remember?
....
(3) Use Google groups, go to comp.lang.python, type "join string" into
the search box, and click on "Search this group". First result is the
current thread; most (or maybe all) of the next six or so will give
you the syntax your were looking for.

(4) Using Google or any other reasonable web searcher, search for the
3 words: Python join string. Any one of the first few hits gives the
answer you were seeking.

Yes, and also, as last resource, I could always consult an oracle
somewhere in the ancient Greece, came back and unsubscribe this NG. :-)

You could just type ",".join(L).
Look at the time we are loosing!

Best regards.
Paulo
Mar 25 '07 #26

P: n/a
En Sun, 25 Mar 2007 15:31:46 -0300, Paulo da Silva
<ps********@esotericaX.ptXescribió:
John Machin escreveu:
>On Mar 25, 12:32 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
>>John Machin escreveu:
>[e.g. it's easy to miss the one line in the
"official" Python tutorial that refers to them] (3) why you were not
using obvious(?) methods to find out for yourself -- much faster than
posing a question on the newsgroup, and you don't have to face the
possibility of an impertinent response :-)

That's the kind of answer I hate. Of course anyone could find an
answer to any questions without posting them to this NG.
But knowing why you didn't follow all those "obvious" paths is valuable -
it can be used to improve the documentation, so the next guy doesn't
*have* to ask here.
You could just type ",".join(L).
Look at the time we are loosing!
It's not a loose at all...

--
Gabriel Genellina

Mar 25 '07 #27

P: n/a
Paulo da Silva escreveu:
John Machin escreveu:
>On Mar 25, 12:32 am, Paulo da Silva <psdasil...@esotericaX.ptXwrote:
>>John Machin escreveu:
...
....
knowledge. If you feel that it is a question that deserves the
honour of your response, just do it. Write it on the stones and
send them down to the common people :-) . ...
Should be read *ordinary* people. This is an error caused by language
differeneces where comum (pt) is ordinary (en) and ordinario (pt
relatively ofensive term) is common (en).
....
>
Yes, and also, as last resource, I could always consult an oracle
somewhere in the ancient Greece, *come* back and unsubscribe this NG. :-)
Regards.
Paulo
Mar 25 '07 #28

This discussion thread is closed

Replies have been disabled for this discussion.