[Tutor] data type conversion for print statement
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 convert var1, var2, var3 all at once? This would avoid errors because of mixed data types. BTW, why does a statment like print var1, var2 automatically add spaces between the variables? Thanks in advance for your help. Timmie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data type conversion for print statement
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 to take a look at the sys.stdout object; it's a file object that corresponds to the output we send to the user. ### import sys sys.stdout open file 'stdout', mode 'w' at 0x2a060 ### As a file object, we can use the methods that files provide: http://www.python.org/doc/lib/bltin-file-objects.html But the one we'll probably want is 'write()': the write() method of a file object lets us send content out, without any adulteration: ## import sys for i in range(5): ... sys.stdout.write('hello' + str(i)) ... hello0hello1hello2hello3hello4 ## We might have to be a little bit more careful with write(), because unlike the print statement, the write() method isn't magical, and won't automatically try to coerse objects to strings. The code above shows that we str() each number, and for good reason. If we try without it, we'll get a TypeError: ## sys.stdout.write(42) Traceback (most recent call last): File stdin, line 1, in ? TypeError: argument 1 must be string or read-only character buffer, not int ## So it just means we'll have to be more aware about the type of a value when we use write(). For some more information, see: http://www.python.org/doc/tut/node9.html#SECTION00920 http://www.python.org/doc/tut/node9.html#SECTION00910 Please feel free to ask more questions. Good luck! On 9/25/07, Tim [EMAIL PROTECTED] 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) how can I convert var1, var2, var3 all at once? This would avoid errors because of mixed data types. BTW, why does a statment like print var1, var2 automatically add spaces between the variables? Thanks in advance for your help. Timmie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data type conversion for print statement
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) how can I convert var1, var2, var3 all at once? Use string formatting: print '%sand this %s needs to check %s' % (var1, var2, var3) The %s format calls str() for its parameter. BTW, why does a statment like print var1, var2 automatically add spaces between the variables? Because it is convenient. Use string formatting to get closer control. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data type conversion for print statement
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 (val1,val2,val3)]) Both don't look very pythonic to me though. :( -- ~noufal ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data type conversion for print statement
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 check %s % (val1,val2,val3) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data type conversion for print statement
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 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. list1=['a','b','c'] for e in list1: print e, a b c # Without whitespace it will look like. print abc abc The language reference says that print is designed to output a space before any object. And some search goes to find and that is controlled by softspace attribute of sys.stdout. Way to print without leading space is using sys.stdout.write() import sys for e in list1: sys.stdout.write(e) abc Reference manual says: A space is written before each object is (converted and) written, unless the output system believes it is positioned at the beginning of a line. This is the case (1) when no characters have yet been written to standard output, (2) when the last character written to standard output is \n, or (3) when the last write operation on standard output was not a print statement. (In some cases it may be functional to write an empty string to standard output for this reason.) Not getting the last part as how you will write a empty string and use print not appending blank space in a single line http://phoe6.livejournal.com/50886.html -- O.R.Senthil Kumaran http://uthcode.sarovar.org ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor