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.

5
  • Show your own effort (code) as properly formatted text in the question. str.replace is a good approach. Commented May 14, 2023 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. If you want to use comments and run it across a set of files, sed would be easier. If you want to do it in Python, the inspect module would be useful (this example isn't exactly what you're looking for, but close). Commented May 14, 2023 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. Commented May 14, 2023 at 15:46
  • print always returns None. Slicing and such isn't necessary when using the replace method. Commented May 14, 2023 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) Commented May 14, 2023 at 16:03

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.