473,804 Members | 4,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Print a list to a string?

I would like to get the results of a print operation placed in a
string. For instance, you can very easily create a list and print it
to stdout:

x = [1,2,3]
print x # Will print [1,2,3]

What if I want the text "[1,2,3]" placed in a string? For instance,
something like:

x = [1,2,3]
str = ''
print str x # x = '[1,2,3]'

Any ideas?

Thanks,
--Steve

Oct 31 '07 #1
4 1817
mrstephengross wrote:
I would like to get the results of a print operation placed in a
string. For instance, you can very easily create a list and print it
to stdout:

x = [1,2,3]
print x # Will print [1,2,3]

What if I want the text "[1,2,3]" placed in a string? For instance,
something like:

x = [1,2,3]
str = ''
print str x # x = '[1,2,3]'

Any ideas?

Thanks,
--Steve

Use str(any-object) or repr(any_object ) to turn an object into a string
representation of that object. Use str for a human friendlier
representation, and repr for a more explicit representation.

Gary Herron

Oct 31 '07 #2
mrstephengross a écrit :
I would like to get the results of a print operation
print is a statement, it doesn't yield any 'result'.
placed in a
string. For instance, you can very easily create a list and print it
to stdout:

x = [1,2,3]
print x # Will print [1,2,3]

What if I want the text "[1,2,3]" placed in a string? For instance,
something like:

x = [1,2,3]
str = ''
You're shadowing the builtin str type here.
print str x # x = '[1,2,3]'

Any ideas?
any of the following should do:

s = "%s" % x
s = repr(x)
Oct 31 '07 #3
On Wed, 31 Oct 2007 22:17:48 +0000, mrstephengross wrote:
I would like to get the results of a print operation placed in a string.
s = str(x)
If you specifically need to capture the output of print, then something
like this:

>>import cStringIO
s = cStringIO.Strin gIO()
print >>s, [1, 2, 1.0/5, 'hello world']
s.getvalue( )
"[1, 2, 0.2000000000000 0001, 'hello world']\n"
(And please, please, PLEASE don't ask why 1/5 is 0.2000...01 until you've
read the FAQs on the Python website. Thank you.)

--
Steven
Oct 31 '07 #4
>import cStringIO
>s = cStringIO.Strin gIO()
print >>s, [1, 2, 1.0/5, 'hello world']
s.getvalue()
Thanks--this works perfectly!

-_Steve

Nov 1 '07 #5

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

Similar topics

30
4180
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn' anything similar you like. Would make my life so much easier. Often I start with a nice and short print statement, then there happen to be more arguments and neat formatting becomes more important and as a result I find myself
23
63703
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two vars, along with their names. I could easily do this: print "myPlace = %s, myTime = %s" % (myPlace, myTime)
1
2130
by: DD | last post by:
I have a mainform with a subform. The main form as a dropdown box "chooseDate", in the afterupdate event i requery the subform so all records with the same date are viewed. Now i only want to print the selected records of the selected month Can any one advise on the code to add to the print button i have tried this StrLinkCriteria = ".forms! = " & Me! this does not work
1
10789
by: Michael Beck | last post by:
I need to select one of about 15 printers, which I have been able to do. Then I need to set that printer as the printer to use, run a Crystal Reports reports, and track if/when the printing job completes. Is there a book or article that can help me? If it is fairly easy, can anyone sen me some code that will get me started. TIA,
0
1837
by: bearophileHUGS | last post by:
There is/was a long discussion about the replacement for print in Python 3.0 (I don't know if this discussion is finished): http://mail.python.org/pipermail/python-dev/2005-September/055968.html There is also a wiki page that collects the ideas: http://wiki.python.org/moin/PrintAsFunction There is the will to keep the printing operation very simple for the most common situation, but at the same time to make it flexible (optional no...
4
1387
by: benjamin.cordes | last post by:
Hi, I have a small problem with my function: printList. I use print with a ',' . Somehow the last digit of the last number isn't printed. I wonder why. import random def createRandomList(param): length = param
2
11043
by: Phoe6 | last post by:
print and softspace in python In python, whenever you use >>>print statement it will append a newline by default. If you don't want newline to be appended, you got use a comma at the end (>>>print 10,) When, you have a list of characters and want them to be printed together a string using a for loop, there was observation that no matter what there was space coming between the characters. No split or join methods helped. print e, a b c
2
2328
by: CC | last post by:
Hi: I've conjured up the idea of building a hex line editor as a first real Python programming exercise. To begin figuring out how to display a line of data as two-digit hex bytes, I created a hunk of data then printed it: ln = '\x00\x01\xFF 456789abcdef' for i in range(0,15):
3
4906
by: Cameron Simpson | last post by:
On 18Aug2008 11:58, Beema Shafreen <beema.shafreen@gmail.comwrote: | In my script i have to print a series of string , so | | print "%s\t%s\t%s\t%s\t%s\t%s\t" %("a","v","t","R","s","f") | | I need to know instead of typing so many %s can i write %6s in python, as | we do in C progm. I hate to tell you this, but "%6s" in C does NOT print 6 strings. It prints 1 string, right justified, in no less that 6 characters.
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10332
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10320
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9150
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4299
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

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.