Python Forum
wierd output syntax for variables - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: wierd output syntax for variables (/thread-26632.html)



wierd output syntax for variables - Zirizo - May-07-2020

I feel as if I'm doing the code correctly and it comes out with an outcome but the outcome for the print function is being weird (I'm using Visual Studio Code on Kali Linux and I'm new).
My Code:
hello = "Hello this is"
print(hello, "code")
Outcome: ('Hello this is', 'code')


RE: wierd output syntax for variables - SheeppOSU - May-07-2020

Are there any errors, because this code looks like it should be working fine.


RE: wierd output syntax for variables - Larz60+ - May-08-2020

you are printing two distinct items, the string in hello, and the literal 'code'
what you want (I think)
is:
print(f"{hello} code")



RE: wierd output syntax for variables - buran - May-08-2020

You are on linux so I expect you have both python2 and python3. python is associated with python2 version
I guess you are running this code with python2. Run it with python3. Use python3 instead of just python.
$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hello = "Hello this is"
>>> print(hello, "code")
('Hello this is', 'code')
$ python3.7
Python 3.7.7 (default, Mar 10 2020, 17:25:08) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hello = "Hello this is"
>>> print(hello, "code")
Hello this is code
on python2 print is a statement, so you in fact you print a tuple

$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hello = "Hello this is"
>>> print hello, "code"
Hello this is code
python2 support ended, so use python3.

And as Larz suggested - it's better to use string formatting - f-strings or str.format() method


RE: wierd output syntax for variables - Zirizo - May-08-2020

(May-08-2020, 03:50 AM)buran Wrote: You are on linux so I expect you have both python2 and python3. python is associated with python2 version
I guess you are running this code with python2. Run it with python3. Use python3 instead of just python.
$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hello = "Hello this is"
>>> print(hello, "code")
('Hello this is', 'code')
$ python3.7
Python 3.7.7 (default, Mar 10 2020, 17:25:08) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> hello = "Hello this is"
>>> print(hello, "code")
Hello this is code
on python2 print is a statement, so you in fact you print a tuple

$ python
Python 2.7.12 (default, Apr 15 2020, 17:07:12) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> hello = "Hello this is"
>>> print hello, "code"
Hello this is code
python2 support ended, so use python3.

And as Larz suggested - it's better to use string formatting - f-strings or str.format() method

I am using Visual Studio Code so i am unable to use the command python3


RE: wierd output syntax for variables - buran - May-08-2020

in vs code you can select what interpreter to use, in number of ways
look at https://python-forum.io/Thread-VS-Code-from-start

or you can use also ctrl+shift+P to access command palette and then choose select python interpreter command
or


RE: wierd output syntax for variables - Zirizo - May-08-2020

(May-07-2020, 10:07 PM)Zirizo Wrote: I feel as if I'm doing the code correctly and it comes out with an outcome but the outcome for the print function is being weird (I'm using Visual Studio Code on Kali Linux and I'm new).
My Code:
hello = "Hello this is"
print(hello, "code")
Outcome: ('Hello this is', 'code')
So, I figured out that when I press run on Visual Studio Code, Bash sends the command python -u "[file path]" now my Kali is set to automatically run python2, however I can override this by typing in python3 -u "[file path]" however I would not like to do this every single time. Is there a way for the python command to run straight python3?


RE: wierd output syntax for variables - Larz60+ - May-08-2020

follow Buran's post and change python version from within VCode.
Once you do this, the selected version will always be used when you press the run button and
also when you right click in code and select 'Run python file in terminal'