[Tutor] how to print array without adding newline
Oscar Benjamin
oscar.j.benjamin at gmail.com
Thu Sep 6 11:44:42 CEST 2012
On Sat, 18 Aug 2012 18:17:16 -0700 (PDT), vickistan
<vicki at stanfield.net> wrote:
> Hello: I am trying to output an array to another program that takes
an array
> as input, but the print statement adds a newline. If it were adding
to each
> individual element, I could solve it easily, but it is adding one
at the end
> of the array. Is there another way to print an array besides
> print arrayname
Yes. You can use:
print arrayname,
Note the trailing comma ',' character at the end of the print
statement. In python 2 this is the normal way to stop print from
adding a newline. It's a silly syntax that has been fixed in python
3. If your using python 3 then it looks like
print(arrayname, end='')
which makes a bit more sense to me.
By the way what your calling an array, python calls a list. In python
the word array is usually used to refer to a number of other things.
As a more general comment: are you the author of both programs? If so
then you can choose a different format for outputting your data. My
default choice would be to print out each url on a separate line,
without the square brackets or commas. You can do that with:
for url in arrayname:
print url
Note that I want print to add the newlines. This is a common way of
working with text files and this is the reason that print adds a
newline.
Oscar
More information about the Tutor
mailing list