0

I have the command below in a .py file

print(r"\n print series")

I want the above command/line to be replaced with a string:

"#series" ie convert the above print command into a comment prefixing the # symbol to the text following the "print" in the command above.

I am new to python. Please help.

This question was closed earlier as it was not clear what I was attempting to do. Hence, the explanation that follows:

The reason for doing this: I want to use print statements in a script for learning python/using as reference. Later, I want to change the print statement into a comment. Hence, I want to replace the beginning of the print statement with a # which will convert it into a comment. As there would be many such conversions, I wanted to do it programmatically using a string.replace or something else. Any suggestions to achieve this in another way are also welcome. Thanks.

user1955215
  • 733
  • 2
  • 11
  • 31
  • Show your own effort (code) as properly formatted text in the question. `str.replace` is a good approach. – Michael Butscher May 14 '23 at 15:35
  • 1
    If the goal is to just disable `print` calls, you can do that [with one of the solutions in this answer](https://stackoverflow.com/questions/8391411/how-to-block-calls-to-print). If you want to use comments and run it across a set of files, [sed would be easier](https://unix.stackexchange.com/questions/99350/how-to-insert-text-before-the-first-line-of-a-file). If you want to do it in Python, the [inspect module](https://stackoverflow.com/questions/38231131/annotating-python-print-output-with-comments) would be useful (this example isn't exactly what you're looking for, but close). – Zac Anger May 14 '23 at 15:37
  • @MichaelButscher, I intended to convert the print command into a string using this line s1 = str(print(r"\n print series")) and then slicing that string and concatenating with # to achieve what I wanted. Unfortunately, s1 is getting assigned the value "None". Hence, unable to do what I want. – user1955215 May 14 '23 at 15:46
  • `print` always returns `None`. Slicing and such isn't necessary when using the `replace` method. – Michael Butscher May 14 '23 at 15:48
  • @MichaelButscher, I tried this to replace the string before slicing: s1.replace("print(r"\n print", "#") but it returns this error:SyntaxError: unexpected character after line continuation character (pointing at the \n in string) – user1955215 May 14 '23 at 16:03

2 Answers2

1

If you want to do this within your script itself (as opposed to doing a find+replace on the code), what I'd suggest would be wrapping the print calls in a function that you can switch off:

DEBUG = True

def debug_print(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)

Then if you have a bunch of calls like:

debug_print(r"\n print series")

they will only print as long as DEBUG is True. When you want to turn off all those print statements, you can just edit that one line:

DEBUG = False

and now nothing prints.

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • Thanks, Tried this. Meets my needs. Effectively, the comments are inside the debug_print statement and can be printed in the output or read from within the script. – user1955215 May 14 '23 at 16:40
0

Here's a simple solution using regex

  1. Store the command as a string in a variable called 'command'.
  2. Substitute the 'print' keyword with a '#' symbol and remove the '\n' character using regex. Making sure to use '.strip()' to remove any leading or trailing spaces.
import re
command = r"\n print series"
comment = re.sub(r"\bprint\b\s*", "#", command.replace(r"\n", "")).strip()
print(comment)

This should output #series, hope this helps.

Joe Wood
  • 1
  • 3