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.
str.replaceis a good approach.printcalls, 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).printalways returnsNone. Slicing and such isn't necessary when using thereplacemethod.