Hello,
I have a print statement where I use concatenation of variables with + to
avoid extra whitespaces. The variables are mixed (float/int).
How can I convert them all to strings to have a clean print statement?
example
print str(var1)+and this +str(var2)+needs to check +str(var3)
how can I
Excerpt from an email Danny Yoo sent to me and the list in 2005. I had the
same question. ;-)
Hi Tom,
The 'print' statement is hardcoded to add a space between elements.
print is meant to make output easy, at the cost of control.
If we need more fine-grained control over output, we may want
Tim wrote:
Hello,
I have a print statement where I use concatenation of variables with + to
avoid extra whitespaces. The variables are mixed (float/int).
How can I convert them all to strings to have a clean print statement?
example
print str(var1)+and this +str(var2)+needs to check
Tim wrote:
Hello,
I have a print statement where I use concatenation of variables with + to
avoid extra whitespaces. The variables are mixed (float/int).
How can I convert them all to strings to have a clean print statement?
example
print str(var1)+and this +str(var2)+needs to check
Noufal Ibrahim wrote:
I suppose you could also do but it's a little less readable
print %sand this %sneeds to check %s%tuple([str(x) for x in
(val1,val2,val3)])
The %s formatter takes care of the string conversion, the list
comprehension is not needed. Just use
print %sand this %sneeds to
The 'print' statement is hardcoded to add a space between elements.
print is meant to make output easy, at the cost of control.
Well, that was a good example. I had prepared Notes for myself also along the
same lines.
print and softspace in python
In python, whenever you use print