Include quotation marks in a variable

I am writing an extremely simple search engine in Python and should use HTML code to create an HTML page with a table on it. This is the code that is provided to me:

<html> <title>Search Findings</title> <body> <h2><p align=center>Search for "rental car"</h2> <p align=center> <table border> <tr><th>Hit<th>URL</tr> <tr><td><b>rental car</b> service<td> <a href="http://www.facebook.com">http://www.avis.com</a></tr> </table> </body> </html> 

This looks great outside of the Python file, but I need to replace the rented car with the KEYWORD variable. The problem occurs when I try to save a line starting with <h2> as a variable to use the .replace method. Python accepts a syntax error due to quotes in the middle. Is there any way to save this as a variable anyway? Or is there another way I should replace these words?

+4
source share
2 answers

Delete it with a backslash or use a single quote string or use """ for long blocks:

 s = '<h2><p align=center>Search for "rental car"</h2>' s = "<h2><p align=center>Search for \"rental car\"</h2>" s = """ <p>This is a <em>long</em> block!</p> <h2><p align=center>Search for "rental car"</h2> <p>It got <strong>lots</strong> of lines, and many "variable" quotation marks.</p> """ 
+5
source

This is a large part of languages ​​such as PHP and Python, interchangeability between single-quoted strings and double-quoted strings, if the internal quotes are opposite to the external quotes. In more detail with Python, when it changes between them, it does not change the way it works. However, with PHP, single-line strings do not process screens other than single quotes and backslashes. Example:

 output = '<h2><p align=center>Search for "' + search + '"</h2>' output = "<h2><p align=center>Search for \"" + search + "\"</h2>" 

Long string strings will not work for concatenation, you will need to use .replace (), which is more expensive than concatenation.

0
source

Source: https://habr.com/ru/post/1446588/


All Articles