Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert Python variable into HTML code in Python script

I have a python script which generates some HTML code to send via email. Within the script there is a variable (tweet), that I would like to include the content of in the email.

Here is the code that generates the HTML for the email:

msg.add_alternative("""\
<!DOCTYPE html>
<html>
    <body>
        <p>Hi Human,</p>
        <p>If you are seeing this, it means that you have received my email. Check out these images!</p>
        <p>Best,</p>
        <p>Simon</p>
        <img src="cid:image1" ><br>
    </body>
</html>
""", subtype='html')

I would like to add the variable ‘tweet’ into the body of the HTML code but dont know how to break the HTML and insert the variable and continue the HTML to finish.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can someone help please…..Id be so grateful….thankyou sooo much.

Cheers
Simon

>Solution :

you have a few options to add variable to a string:

  1. using format
"data is {0}, I'm {1}".format(name, id)
  1. using f"text text {your variable}"

I`m not sure where you want to add your variable so I hope this will help

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading