how to put html as a string in python in variable

Question:

I have a html boiler plate in which i want to put some variables later when i get it so just paste the html as a string and insert variables in that but doing this giving me error that string literal is undetermined this is logically a string

txt = f"
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{title}</title>
</head>
<body>
    
</body>
</html>"
Asked By: sarangkkl

||

Answers:

You should use """ your string here """ if you intend to write a string on multiple lines in python, so your txt variable should belike :

txt = f"""
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>{title}</title>
    </head>
    <body>
        
    </body>
    </html>"""
Answered By: mrCopiCat

Multi-line strings need triple strings, so you could do

txt = f"""
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{title}</title>
</head>
<body>
    
</body>
</html>"""
Answered By: eth
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.