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 4 1798
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
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)
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.StringIO() print >>s, [1, 2, 1.0/5, 'hello world'] s.getvalue()
"[1, 2, 0.20000000000000001, '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
>import cStringIO
>s = cStringIO.StringIO() print >>s, [1, 2, 1.0/5, 'hello world'] s.getvalue()
Thanks--this works perfectly!
-_Steve This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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'...
|
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...
|
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...
|
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...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
Hello Experts!
I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| |