[Tutor] Python printing parentheses and quotes
Hello! I was just wondering if anybody encountered an issue where the Python interpreter was changing how it interprets print statements. So I'm using default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the "python script.py" command. 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") ("") So it was printing parentheses and quotes. The above result might not be exactly accurate because I didn't save the output, but it was something generally like that. Then I changed a few small things in the script but nothing big ("import sys", adding "#!usr/bin/env python", and accidentally trying to close the Python interpreter by using ^C multiple times). I didn't really change too much though but maybe I changed something simple that I didn't know would cause something like that. Is there any reason why Python would start to print parentheses and quotes like that. Thank you! Best Wishes, Sai Allu P.S. After I upgrade to Python3 this started working. When I kept Python2, then I was able to add an extra statement like Print("Yo") in Sublime Text and this printed Yo just like expected. But actually maybe I had Python3 before I added this print statement, I'm not too sure. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python printing parentheses and quotes
But then how come it was working earlier for me without that import statement. Python doesn't interpret it as a statement exclusively, before it worked fine as a function. Best Wishes, Sai Allu From: Mats Wichmann Sent: Monday, June 10, 2019 11:12 AM To: Sai Allu; tutor@python.org Subject: Re: [Tutor] Python printing parentheses and quotes On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python > interpreter was changing how it interprets print statements. So I'm using > default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the > "python script.py" command. > > 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") > > ("") > > So it was printing parentheses and quotes. The above result might not be > exactly accurate because I didn't save the output, but it was something > generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python printing parentheses and quotes
Actually I'm pretty sure what happened was that the "#! usr/bin/python" was in a module that was being imported. So the Python interpreter cached it or somehow crashed randomly, which meant that the print was working as a keyword instead of a function. But when I removed that "#! usr/bin/python" line and then rewrote the print statements, it went back to working normally. Thank you for the help though! Sai Allu ________ From: Sai Allu Sent: Monday, June 10, 2019 11:53 AM To: Mats Wichmann; tutor@python.org; Deepak Dixit Subject: Re: [Tutor] Python printing parentheses and quotes But then how come it was working earlier for me without that import statement. Python doesn't interpret it as a statement exclusively, before it worked fine as a function. Best Wishes, Sai Allu From: Mats Wichmann Sent: Monday, June 10, 2019 11:12 AM To: Sai Allu; tutor@python.org Subject: Re: [Tutor] Python printing parentheses and quotes On 6/10/19 10:50 AM, Sai Allu wrote: > Hello! > > I was just wondering if anybody encountered an issue where the Python > interpreter was changing how it interprets print statements. So I'm using > default Python on Mac OSX (2.7.10 I'm pretty sure) and running with the > "python script.py" command. > > 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") > > ("") > > So it was printing parentheses and quotes. The above result might not be > exactly accurate because I didn't save the output, but it was something > generally like that. In Python 2, print is a statement. In Python 3 it's a function and behaves like you're expecting. However, the behavior you're seeing is odd (printing parentheses is a surprise unless there's more going on than you've listed) If you want them consistent across both versions, add a statement at the very top: from __future__ import print_function ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python printing parentheses and quotes
Thank you Cameron, This was something that I was working on last week and I've been able to fix the script since then. I was just curious why the previous version did that and you might be right, thank you for the help though! Best Wishes, Sai Allu From: Cameron Simpson Sent: Monday, June 10, 2019 5:34 PM To: Sai Allu Cc: Mats Wichmann; tutor@python.org; Deepak Dixit Subject: Re: [Tutor] Python printing parentheses and quotes On 10Jun2019 19:04, Sai Allu wrote: >Actually I'm pretty sure what happened was that the "#! usr/bin/python" was in >a module that was being imported. So the Python interpreter cached it or >somehow crashed randomly, which meant that the print was working as a keyword >instead of a function. > >But when I removed that "#! usr/bin/python" line and then rewrote the print >statements, it went back to working normally. My personal suspicision is that what you might have been doing is this (notice the trailing comma): print("",) or maybe: print("this", that") Look: % python Python 2.7.16 (default, Apr 1 2019, 15:01:04) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) ('',) >>> % python3 Python 3.7.3 (default, Mar 30 2019, 03:38:02) [Clang 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print("",) >>> What is happening? In Python 2, print is a statement unless you use the __future__ import already mentioned. That means that this: print("",) is a "print" of the expression ("",), which is a 1-tuple, and gets printed as a tuple. The more likely scenario is when you're printing mulitple things: print("this", "that") which is still a "print" of a tuple. However, in Python 3 print is a function which means that the brackets are part of the function call. So this: print("") or: print("this", "that") is a call to the "print()" function, passing one or two arguments, which get printed. And printing "" (the former case) is an empty string. Please revisit your code can test this. Subtle issues like this are why we like to receive _exact_ cut/paste of your code and the matching output, not a retype of what you thought you ran. If you encounter something weird like this, it is well worth your time (and ours) if you make a tiny standalone script showing the problem, as small as possible. Then paste it into your message and paste in the output (this list drops attachments). That we we can all run exactly the same code, and solve your actual problem. And start always using: from __future__ import print_function in Python if you're using print. That will make your prints behave the same regardless if whether they are using Python 2 or 3. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor