Is there a way to encase a text which contains lots of operators/formatting and ignore all interpretation?

Question:

So for example:

txt = r"<img alt="image tags" height="652" id="image" onclick="Note.toggle();" src="https://us.example.com//images/1/5d2.gif?1" width="600"/>"

Now, clearly, this doesn’t work for a lot of reasons, namely quotations and escape characters. It’s possible to manually format each txt but this is intended to automatically find img from webpages.

I think Regex might be a clear solution here but if there is just some character to place before and after then that would be ideal. I’m a lot more used to C++ where this would have been trivial, I apologize for my lack of experience.

Asked By: Jones Crimson

||

Answers:

I believe using triple quotes will solve the problem you are facing:

txt = r"""<img alt="image tags" height="652" id="image" onclick="Note.toggle();" src="https://us.example.com//images/1/5d2.gif?1" width="600"/>"""

Triple quotes allow you to have strings spanning multiple lines and also avoid difficulties in using single or double quotes in the string. The r prefix escapes any special characters (e.g., backslash) in the string.

Answered By: sebtheiler

I think what you are searching for is r"""..."""

txt = r"""<img alt="image tags" height="652" id="image" onclick="Note.toggle();" src="https://us.example.com//images/1/5d2.gif?1" width="600"/>"""
Answered By: Rabinzel

In this case you could simply use single quotation marks.

txt = r'<img alt="image tags" height="652" id="image" onclick="Note.toggle();" src="https://us.example.com//images/1/5d2.gif?1" width="600"/>'
Answered By: Klaus
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.