[Tutor] data type conversion for print statement

Noufal Ibrahim noufal at airtelbroadband.in
Tue Sep 25 21:02:11 CEST 2007

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 "+str(var3)

Well, if they're all ints or floats, you could do something like
print "%fand this %fneeds to check %f"%(val1,val2,val3) but you'll get 
decimal points for everything.

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 

Both don't look very pythonic to me though. :(


More information about the Tutor mailing list