Reputation:
I have a really noob question here , in php if u wanted to print something inside html you did something like this
<body>
<div>
<?
echo 'Hi i am inside of a div';
?>
</div>
</body>
How do i do that in python?
Upvotes: 1
Views: 2561
Reputation: 474231
In a simple case you can use string formatting. Put a placeholder into the string and use format()
to substitute the placeholder with the actual text:
html = """
<body>
<div>
{text}
</div>
</body>
"""
print html.format(text='Hi i am inside of a div')
In case of complex HTML structures, you can make use of template languages, like jinja2
. You would basically have an html template which you would render with the context provided:
import jinja2
env = jinja2.Environment(loader=jinja2.FileSystemLoader('.'))
template = env.get_template('index.html')
print template.render(text='Hi i am inside of a div')
where index.html
contains:
<body>
<div>
{{ text }}
</div>
</body>
Upvotes: 3