Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print output not working
#1
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?
Larz60+ write Jan-14-2021, 08:33 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags in future posts.
Reply
#2
try

print(f'Hello World {inv}')

or

print('Hello World', inv)
Reply
#3
(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
Reply
#4
(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.
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
>>>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?
buran write Jan-15-2021, 10:00 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#6
please disregard, i know where i am wrong. According to my notes i have to put = but they are wrong. Please accept my apologies
lufbrarunner likes this post
Reply
#7
There are multiple issues/misunderstandings, apart from not using BBCode tags right.

  1. 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.
  2. 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).
  3. 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.
  4. @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.

  5. There are multiple ways to construct a string from different parts. The f-strings are the latest addition to so called string-formatting`.

  6. 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.
  7. 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.
  8. 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.
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
#8
(Jan-15-2021, 10:28 AM)buran Wrote: There are multiple issues/misunderstandings, apart from not using BBCode tags right.

  1. 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.
  2. 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).
  3. 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.
  4. @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.

  5. There are multiple ways to construct a string from different parts. The f-strings are the latest addition to so called string-formatting`.

  6. 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.
  7. 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.
  8. 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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,075 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to output one value per request of the CSV and print it in another func? Student44 3 1,321 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,278 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 10,902 Jan-11-2022, 07:04 PM
Last Post: snippsat
Photo print output none 3lnyn0 4 1,820 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  output correction using print() function afefDXCTN 3 11,063 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,917 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,105 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Output with none, print(x) in function Vidar567 3 2,498 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  Print output in single file using pramika loop deepakkhw 1 2,064 Jul-11-2020, 11:57 AM
Last Post: j.crater

Forum Jump:

User Panel Messages

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