473,465 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

formatting list -> comma separated

given d:

d = ["soep", "reeds", "ook"]

I want it to print like

soep, reeds, ook

I've come up with :

print ("%s"+", %s"*(len(d)-1)) % tuple(d)

but this fails for d = []

any (pythonic) options for this?

Robert

Jul 9 '08 #1
3 942
On Wed, Jul 9, 2008 at 3:23 PM, Robert <rw*******@xs4all.nlwrote:
given d:
d = ["soep", "reeds", "ook"]

I want it to print like
soep, reeds, ook
use the join() method of strings, like this:
>>d = ["soep", "reeds", "ook"]
', '.join(d)
'soep, reeds, ook'
>>d = []
', '.join(d)
''
>>>
--
Jerry
Jul 9 '08 #2
On Jul 9, 8:23*pm, "Robert" <rw.seg...@xs4all.nlwrote:
given d:

d = ["soep", "reeds", "ook"]

I want it to print like

soep, reeds, ook

I've come up with :

print ("%s"+", %s"*(len(d)-1)) % tuple(d)

but this fails for d = []

any (pythonic) options for this?
print ', '.join(d)

--
Paul Hankin
Jul 9 '08 #3

Robert wrote:
given d:

d = ["soep", "reeds", "ook"]

I want it to print like

soep, reeds, ook

I've come up with :

print ("%s"+", %s"*(len(d)-1)) % tuple(d)

but this fails for d = []

any (pythonic) options for this?

Robert

--
http://mail.python.org/mailman/listinfo/python-list
================================
the old fashion way would be:

d = ["soep", "reeds", "ook"]
if len(d) 0:
print ("%s"+", %s"*(len(d)-1)) % tuple(d)

# if d= [] then print stmnt bypassed

Steve
no******@hughes.net
Jul 10 '08 #4

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

Similar topics

3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
2
by: jodyblau | last post by:
I'm not certain that what I am trying to do is possible; in any event I haven't been able to figure it out. Here is what I am trying to do: I have one table that has a list of cases I'm working...
8
by: gopal | last post by:
Hi I am trying to write to log file but the formatting of string is not properly aligned in log file result The results looks some thing like this OK move.xml Successful OK ...
4
by: slinky | last post by:
Thanks in advance... I have a continuous style form with a field for each record called "STATUS". I simply want to have the form load and if the value of the textbox is "Inactive" I want the...
2
by: Tim Chase | last post by:
Is there an easy way to make string-formatting smart enough to gracefully handle iterators/generators? E.g. transform = lambda s: s.upper() pair = ('hello', 'world') print "%s, %s" % pair #...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.