如何将html作为字符串放入变量python中



我有一个html锅炉板,当我得到它时,我想在其中放入一些变量,所以只需将html粘贴为字符串并在其中插入变量,但这样做会给我一个错误,即字符串文字不确定,这在逻辑上是一个字符串

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

如果您打算在python中的多行上写入字符串,则应使用""" your string here """,因此您的txt变量应为:

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

多行字符串需要三个字符串,因此可以执行

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

最新更新