Can you create a global variable which you can check to decide how much you want to print ? By doing that you can control the amount of logging as you require.

  if printLevel > 3:
      print("Bobby hits a fly ball for an out")
Answer from Cobusve on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how do i tell python not to print something that is stored in a value?
r/learnpython on Reddit: How do I tell python not to print something that is stored in a value?
December 23, 2020 -
#This is a game where a user can skip playing if they guess a magic word from the star, it's a word they dont even no exist. I want to know how can I not print a value if this word is guessed.

name = input("What is your name? ")
welcome = print(f"Hi {name} I am thinking of a number between 1, 20!")
print(welcome)

#here I want to creat a magic word, where if placed in the name input some prints telling the user they guessed the magic word pop out. soo..

if name == "cheat word":
    print("wow! you guessed the cheat word, and the secret number is (#NohelpNeeded)

# What I want help with, is when the cheat word is guessed, i don't want the print stored in value welcome to print out, so just the print saying ("wow! you...etc") gets printed. 

Thank you

🌐
Python.org
discuss.python.org › python help
Why is the print() not working! - Python Help - Discussions on Python.org
July 25, 2021 - Starting with Python, trying to do a sum inside a for loop and then print the total Outside of the for loop. Have the right indentation in place to do this but still print() returns a Syntax error! Please suggest where is this going wrong. File “”, line 3 print(total) ^ SyntaxError: invalid syntax ```py3 YOUR CODE GOES HERE total=0 for line in open(‘/home/values’): total=total + int(line.replace(‘,’,‘’)) print(total) ```
🌐
Python.org
discuss.python.org › python help
Python not printing - Python Help - Discussions on Python.org
August 24, 2021 - Hi I’m trying make a code where it prints the last lines of a file, and when i run the code, it runs fine but no output shows. I’m using the readline() command. Here is the code: with open(“MergeFiles.txt”) as File1: print("How many lines do you have?") File1Len = len(File1.readlines()) print("File 1 has: "+str(File1Len)+" Lines") print("File 1's last line is: "+File1.read(File1Len)) File1.flush() File1.close() with open ("MergeFiles2.txt") as File2: File2Len = len(File2.readlines(...
🌐
Quora
quora.com › How-do-I-make-Python-not-print-a-statement-if-it-is-not-true-using-the-if-condition
How to make Python not print a statement if it is not true using the if condition - Quora
Answer (1 of 2): How do I make Python not print a statement if it is not true using the if condition? OK, let’s clarify your question. 1. You’re asking how to “not print” something. So let’s reword this as “In Python, how do I avoid printing…” 2. What you are trying not to print is “not true”, ...
🌐
freeCodeCamp
forum.freecodecamp.org › python
Python code not printing output - Python - The freeCodeCamp Forum
November 19, 2016 - # your code goes here def calculate(m,n): result = m * n return result print result calculate(2,3) This is python code and there is proper indentation in it ,as have tried to put it here also but editor is not supporting .Any way it is running successfully over my editor but it is not printing ...
🌐
Reddit
reddit.com › r/python › why is ´print´ not recommended in linters?
r/Python on Reddit: Why is ´print´ not recommended in linters?
December 4, 2023 -

I am writing a mini-program for basic payment calculation, and after the calculation the results are printed in the terminal. However, I get the following warnings from Ruff (the Python linter that I use):

src/pf_example/food_payment.py:39:5: T201 print found src/pf_example/food_payment.py:51:5: T201 print found src/pf_example/food_payment.py:52:5: T201 print found src/pf_example/food_payment.py:54:5: T201 print found src/pf_example/food_payment.py:56:9: T201 print found

I know that I can turn off this check in the settings, BUT I don't why print is bad in the code. What would be the alternatives if not using print?

Find elsewhere
🌐
Medium
medium.com › build-useful-stuff › no-print-python-42abc193ada4
No-Print Python. Stop using print to debug. | by Hamza | The Binary Bin | Medium
September 5, 2021 - To keep us from accidentally using print , we’re going to try to make it so that every time we call the function, it will simply print “Nope.” This should act as a mild rebuke and hopefully discipline us into never using print again. Feel free to print a more stern message or throw errors if you need further reinforcement. With Python, you can reassign almost anything.
Top answer
1 of 14
179

Use with

Based on @FakeRainBrigand solution I'm suggesting a safer solution:

import os, sys

class HiddenPrints:
    def __enter__(self):
        self._original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout.close()
        sys.stdout = self._original_stdout

Then you can use it like this:

with HiddenPrints():
    print("This will not be printed")

print("This will be printed as before")

This is much safer because you can not forget to re-enable stdout, which is especially critical when handling exceptions.

Bad practice: Without with

The following example uses enable/disable prints functions that were suggested in previous answer.

Imagine that there is a code that may raise an exception. We had to use finally statement in order to enable prints in any case.

try:
    disable_prints()
    something_throwing()
    enable_prints() # This will not help in case of exception
except ValueError as err:
    handle_error(err)
finally:
    enable_prints() # That's where it needs to go.

If you forgot the finally clause, none of your print calls would print anything anymore.

It is safer to use the with statement, which makes sure that prints will be reenabled.

Note: It is not safe to use sys.stdout = None, because someone could call methods like sys.stdout.write()

2 of 14
171

Python lets you overwrite standard output (stdout) with any file object. This should work cross platform and write to the null device.

import sys, os

# Disable
def blockPrint():
    sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
    sys.stdout = sys.__stdout__


print 'This will print'

blockPrint()
print "This won't"

enablePrint()
print "This will too"

If you don't want that one function to print, call blockPrint() before it, and enablePrint() when you want it to continue. If you want to disable all printing, start blocking at the top of the file.

🌐
Quora
quora.com › Why-my-python-code-snippet-is-printing-none-along-with-the-answer
Why my python code snippet is printing none along with the answer? - Quora
... .. but when you did write “ print fact() “ it will print “ none” as your function isnt returning anything ... A mechanical engineer who studied CS for 4 years. · 9y · you just need to call the value not “print” it.
🌐
Quora
quora.com › My-python-code-is-not-printing-my-function-when-I-call-it-what-should-I-check
My python code is not printing my function when I call it , what should I check? - Quora
Answer (1 of 3): The most usual reasons that your function does not give an printed output are: 1. You are not telling it to: i.e. use [code ]print(fn())[/code] rather than just [code ]fn()[/code] 2. Your function is not returning anything on ...
🌐
Reddit
reddit.com › r/learnpython › getting "none" output in terminal when running a defined function
r/learnpython on Reddit: Getting "None" output in terminal when running a defined function
February 3, 2022 -

So I am currently trying to learn some python by using online resources. I am currently going through the ThinkPython2 book and got to a part in chapter 3 that goes over defining your own function. Currently when I run the code the function will print out correctly but will add a random "None" after it is done printing the lines.

def print_lyrics():
    print("Somebody once told me")
    print("The world is gonna roll me")

print(print_lyrics())

<file directory on this line>
Somebody once told me
The world was gonna roll me
None

Process finished with exit code 0

Any help?

🌐
Reddit
reddit.com › r/learnpython › how to prevent a null variable from printing as "none"?
How to prevent a null variable from printing as "None"? : r/learnpython
October 25, 2023 - Lastly, I don’t understand what you mean when you say that this is not extendable. How is OP’s or any of the other solutions here more extendable than this one? If you need to change the conditions, then you’ll need to refactor the code. ... dog = True cat = True x = 'woof' if dog else None y = 'meow' if cat else None print(*filter(None, [x, y]))