473,573 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join strings - very simple Q.

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
27 2825
Paulo da Silva <ps********@eso tericaX.ptXwrit es:
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
On Mar 23, 2:37 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
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
Paul Rudin <pa********@ntl world.comwrites :
Paulo da Silva <ps********@eso tericaX.ptXwrit es:
>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
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
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
On Mar 24, 5:37 am, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
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("".joi n)
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
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
On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
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
On Mar 24, 5:59 am, "Dustan" <DustanGro...@g mail.comwrote:
On Mar 23, 1:30 pm, Paulo da Silva <psdasil...@eso tericaX.ptXwrot e:
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10511
by: Peter L. Buschman | last post by:
I'm trying to think in Python, but am stumped here... What is the equivalent for the following if you are dealing with a tuple of non-strings? >>> import string >>> foo = ( '1', '2', '3' ) >>> string.join( foo, '.' ) '1.2.3'
16
2437
by: Jim Hefferon | last post by:
Hello, I'm getting an error join-ing strings and wonder if someone can explain why the function is behaving this way? If I .join in a string that contains a high character then I get an ascii codec decoding error. (The code below illustrates.) Why doesn't it just concatenate? I'm building up a web page by stuffing an array and then...
46
2431
by: Leo Breebaart | last post by:
I've tried Googling for this, but practically all discussions on str.join() focus on the yuck-ugly-shouldn't-it-be-a-list-method? issue, which is not my problem/question at all. What I can't find an explanation for is why str.join() doesn't automatically call str() on its arguments, so that e.g. str.join() would yield "1245", and ditto for...
14
15013
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a...
3
1683
by: Sandra-24 | last post by:
I'd love to know why calling ''.join() on a list of encoded strings automatically results in converting to the default encoding. First of all, it's undocumented, so If I didn't have non-ascii characters in my utf-8 data, I'd never have known until one day I did, and then the code would break. Secondly you can't override (for valid reasons) the...
6
11297
by: johnny | last post by:
How do I join two string variables? I want to do: download_dir + filename. download_dir=r'c:/download/' filename =r'log.txt' I want to get something like this: c:/download/log.txt
6
1562
by: Matt Mackal | last post by:
I have an application that occassionally is called upon to process strings that are a substantial portion of the size of memory. For various reasons, the resultant strings must fit completely in RAM. Occassionally, I need to join some large strings to build some even larger strings. Unfortunately, there's no good way of doing this without...
54
8316
by: bearophileHUGS | last post by:
Empty Python lists don't know the type of the items it will contain, so this sounds strange: 0 Because that may be an empty sequence of someobject: 0 In a statically typed language in that situation you may answer the
0
7699
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7788
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8066
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3733
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.