Python Forum
wierd output syntax for variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wierd output syntax for variables
#1
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')
Reply
#2
Are there any errors, because this code looks like it should be working fine.
Reply
#3
you are printing two distinct items, the string in hello, and the literal 'code'
what you want (I think)
is:
print(f"{hello} code")
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
(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?
Reply
#8
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 941 Jan-18-2023, 10:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020