Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print output not working
#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


Messages In This Thread
Print output not working - by xninhox - Jan-14-2021, 10:45 AM
RE: Print output not working - by Axel_Erfurt - Jan-14-2021, 12:09 PM
RE: Print output not working - by xninhox - Jan-15-2021, 09:04 AM
RE: Print output not working - by buran - Jan-15-2021, 09:11 AM
RE: Print output not working - by xninhox - Jan-15-2021, 09:58 AM
RE: Print output not working - by xninhox - Jan-15-2021, 10:05 AM
RE: Print output not working - by buran - Jan-15-2021, 10:28 AM
RE: Print output not working - by xninhox - Jan-16-2021, 09:42 AM

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,169 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,399 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to print the output of a defined function bshoushtarian 4 1,368 Sep-08-2022, 01:44 PM
Last Post: deanhystad
Sad Want to Save Print output in csv file Rasedul 5 11,210 Jan-11-2022, 07:04 PM
Last Post: snippsat
Photo print output none 3lnyn0 4 1,885 Nov-01-2021, 08:46 PM
Last Post: 3lnyn0
  output correction using print() function afefDXCTN 3 11,238 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx
  print (output) taaperaban 3 1,972 Sep-03-2021, 04:23 PM
Last Post: deanhystad
  print function output wrong with strings. mposwal 5 3,215 Feb-12-2021, 09:04 AM
Last Post: DPaul
  Output with none, print(x) in function Vidar567 3 2,573 Nov-24-2020, 05:40 PM
Last Post: deanhystad
  Print output in single file using pramika loop deepakkhw 1 2,122 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