[Tutor] Python printing parentheses and quotes
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Jun 10 14:38:24 EDT 2019
On 10/06/2019 17:50, Sai Allu wrote:
> Basically what happened was that I had a few lines in the script like this
> ip = "10.41.17.237"
> print(" Welcome to Squid Monitoring for ", ip)
> print("")
>
> and the output was like this
>
> (" Welcome to Squid Monitoring for 10.41.17.237")
>
> ("")
Are you sure? Is that a cut n paste or just how you think you remember
it? The reason i ask is that its not what i see and not what I'd expect.
In Python v2 print is a statement which means that Python sees your
first print line like:
print (" Welcome to Squid Monitoring for ", "10.41.17.237")
That is it thinks you want it to print a tuple of 2 strings and what I
see as output is:
(' Welcome to Squid Monitoring for ', '10.41.17.237')
Which is a tuple of 2 strings...
Now if I remove the parentheses it looks like:
print " Welcome to Squid Monitoring for ", "10.41.17.237"
Which is telling Python to print two strings joined by a space.
And I see the output:
Welcome to Squid Monitoring for 10.41.17.237
And in both cases the second print just prints out an empty
string with no quotes.
Are you sure that's not what you saw?
> P.S. After I upgrade to Python3 this started working.
In Python 3 print is a function so it needs the parentheses.
Without them it will report a syntax error. So for Python 3
your original code is correct.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list