Print output not working - xninhox - Jan-14-2021
Dear All,
i started studying python and i have been facing some issues with command print. I will do an exaple:
>>>print=('Hello World') + inv
>>> When i put the above command in the idle i do not get the output string which should be >>>"Hello World"
Can you help me please?
RE: Print output not working - Axel_Erfurt - Jan-14-2021
try
print(f'Hello World {inv}')
or
print('Hello World', inv)
RE: Print output not working - xninhox - Jan-15-2021
(Jan-14-2021, 12:09 PM)Axel_Erfurt Wrote: try
print(f'Hello World {inv}')
or
print('Hello World', inv)
Dear Axel,
thanks for your help,i really appreciated that. Finally it's working by this option (f'Hello World') + inv
RE: Print output not working - buran - Jan-15-2021
(Jan-15-2021, 09:04 AM)xninhox Wrote: Finally it's working by this option (f'Hello World') + inv can you show your exact code, so that we can explain better why this is far from optimal.
RE: Print output not working - xninhox - Jan-15-2021
>>>print=('Hello World')
>>>
>>>print=(f'Hello World')
>>> Still not working with both options. The second one was working at the first attempt but now i have the same issue.
Dear moderator, i hope i used the BBcode properly. If not, sorry about that.
Can anyone help me and clarify why i have been facing this issue please?
RE: Print output not working - xninhox - Jan-15-2021
please disregard, i know where i am wrong. According to my notes i have to put = but they are wrong. Please accept my apologies
RE: Print output not working - buran - Jan-15-2021
There are multiple issues/misunderstandings, apart from not using BBCode tags right.
- You are using python interactive shell at the moment (that is having >>> when you write code). This is good for experiments, but normally you would write your code in a file, save that file with
.py extension and run it. Using interactive shell makes a difference because each line is evaluated the moment you hit Enter . In other words - at the moment you don't print. I guess it "worked" because you missed the print= part and it just evaluated the expression (f'Hello World') + inv .
print is a function, you need to call it, supplying 1 or more arguments (note, there are functions that will take no arguments, but print requires at least one).
- when you do
print=('Hello World') you are doing an assignment. Note that in (Hello World') the brackets are redundant. it's the same as if you are doing print='Hello World' . Now, name print has value 'Hello World' (it's a poor name, because it overrides the built-in function, but I know you didn't mean to).
>>> print=('Hello World')
>>> print
'Hello World'
>>> Note, this is NOT printing, it evaluate and display name print .
- @Axel_Erfurt in his post #2 show you what you have to do. There are other possible approaches. In his first example, he pass f-string (from formatted-string)
f'Hello World {inv}' as single argument to print() function. That is what will be printed. Note that we assume you already have defined name inv . If you did not - this will cause error.
>>> inv = 'World'
>>> f'Hello {inv}'
'Hello World' Again, this is not printing, it just evaluates the f'Hello {inv}' and replace {inv} with value of inv and display the result.
In the second he pass arguments to print function. As a result, when print()` function is executed, it prints all arguments, separated by space. You can change that behavior and specify different separator, but that's yet another thing.
- There are multiple ways to construct a string from different parts. The f-strings are the latest addition to so called string-formatting`.
- now, you need first to restart your python interactive shell, if you didn't do so yet, so that you can fix the problem with
print() function being overriden by your assignments.
>>> inv = 'World'
>>> print(f'Hello {inv}')
Hello World now, this is printing. Do you see the difference? in the previous example there were single quotes around Hello World and here there are none.
- As I said better work in a file, not in the interactive shell, so that you don't lose your work, you can make small changes and run the file again and see what happens.
- In your post you said it worked with
(f'Hello World') + inv . I already mentioned that brackets are redundant here, but also the f is redundant, because you are not doing any string formatting (no {} inside the string). So it's the same as 'Hello World' + inv . This is string concatenation and it will work only if inv is also str , if it is e.g. number int it will not work.
All that said, I strongly advise you to find a tutorial and follow it, so that you get familiar with the very basic concepts. Otherwise it will be hard and time-consuming.
For the tags - look again at the link in my moderator note.
EDIT: OP solved their problem, but I already have written this extensive post, so I will keep it.
RE: Print output not working - xninhox - Jan-16-2021
(Jan-15-2021, 10:28 AM)buran Wrote: There are multiple issues/misunderstandings, apart from not using BBCode tags right.
- You are using python interactive shell at the moment (that is having >>> when you write code). This is good for experiments, but normally you would write your code in a file, save that file with
.py extension and run it. Using interactive shell makes a difference because each line is evaluated the moment you hit Enter . In other words - at the moment you don't print. I guess it "worked" because you missed the print= part and it just evaluated the expression (f'Hello World') + inv .
print is a function, you need to call it, supplying 1 or more arguments (note, there are functions that will take no arguments, but print requires at least one).
- when you do
print=('Hello World') you are doing an assignment. Note that in (Hello World') the brackets are redundant. it's the same as if you are doing print='Hello World' . Now, name print has value 'Hello World' (it's a poor name, because it overrides the built-in function, but I know you didn't mean to).
>>> print=('Hello World')
>>> print
'Hello World'
>>> Note, this is NOT printing, it evaluate and display name print .
- @Axel_Erfurt in his post #2 show you what you have to do. There are other possible approaches. In his first example, he pass f-string (from formatted-string)
f'Hello World {inv}' as single argument to print() function. That is what will be printed. Note that we assume you already have defined name inv . If you did not - this will cause error.
>>> inv = 'World'
>>> f'Hello {inv}'
'Hello World' Again, this is not printing, it just evaluates the f'Hello {inv}' and replace {inv} with value of inv and display the result.
In the second he pass arguments to print function. As a result, when print()` function is executed, it prints all arguments, separated by space. You can change that behavior and specify different separator, but that's yet another thing.
- There are multiple ways to construct a string from different parts. The f-strings are the latest addition to so called string-formatting`.
- now, you need first to restart your python interactive shell, if you didn't do so yet, so that you can fix the problem with
print() function being overriden by your assignments.
>>> inv = 'World'
>>> print(f'Hello {inv}')
Hello World now, this is printing. Do you see the difference? in the previous example there were single quotes around Hello World and here there are none.
- As I said better work in a file, not in the interactive shell, so that you don't lose your work, you can make small changes and run the file again and see what happens.
- In your post you said it worked with
(f'Hello World') + inv . I already mentioned that brackets are redundant here, but also the f is redundant, because you are not doing any string formatting (no {} inside the string). So it's the same as 'Hello World' + inv . This is string concatenation and it will work only if inv is also str , if it is e.g. number int it will not work.
All that said, I strongly advise you to find a tutorial and follow it, so that you get familiar with the very basic concepts. Otherwise it will be hard and time-consuming.
For the tags - look again at the link in my moderator note.
EDIT: OP solved their problem, but I already have written this extensive post, so I will keep it. Hi Buran,
thank you for this heads up, i love this community and sorry again for the wrong BB code used
|