Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在多行Python字符串中使用HTML中的Python变量_Python_Html - Fatal编程技术网

在多行Python字符串中使用HTML中的Python变量

在多行Python字符串中使用HTML中的Python变量,python,html,Python,Html,我正在尝试使用python编写HTML代码,并在浏览器中执行它。这是我的密码: import webbrowser f = open('image.html','w') message = """<html> <head></head> <body><img src="URL"></body> </html>""" f.write(message) f.close() filename = 'file:/

我正在尝试使用python编写HTML代码,并在浏览器中执行它。这是我的密码:

import webbrowser

f = open('image.html','w')

message = """<html>
<head></head>
<body><img src="URL"></body>
</html>"""

f.write(message)
f.close()

filename = 'file:///home/pi/' + 'image.html'
webbrowser.open_new_tab(filename)
导入网络浏览器
f=open('image.html','w')
message=”“”
"""
f、 写(信息)
f、 关闭()
filename='1file:///home/pi/“+”image.html”
webbrowser.打开新选项卡(文件名)
简单的代码,像一个魅力工程

现在我想制作一个小UI,这样用户就可以输入URL了。所以我的问题是,我可以把Python变量放到HTML代码中而不是URL中吗? 例如:

a = ¨webpage.com/image.jpg¨
...
<img src="a">
...
a=¨webpage.com/image.jpg¨
...
...
当然,我知道语法是非常错误的,我只是想给你一个例子,说明我正在努力实现的目标。
干杯

如果您使用的是python 3.6+,则可以使用:


您需要研究变量插值(或者更一般地说,字符串格式)。看一看。给你一个简单的例子:

foo = "hello"
bar = """world
%s""" % foo

print bar
…将输出

hello
world

字符串格式听起来像是一个很有用的主题,您可以仔细阅读:
foo = "hello"
bar = """world
%s""" % foo

print bar
hello
world