Simple Python Variables Examples

In this post I am going to show you 2 simple ways of using variables in your python html files.

The 1st way is using a text, string, variable

#!/usr/bin/python

msg='Python Text Variable.'

print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>Simple Python Text Variable</title>";
print "</head><body>";
print "<p>" + msg + "</p>";
print "</body></html>";

Here is a working example:

http://pythoninhtmlexamples.com/files/python_text_variable.py

Way number 2 is to use a integer in your html file:

#!/usr/bin/python

n1 = 2
n2 = 4

#here I am casting my variables as an integer (numeric) value so I can calculate them.
total= (int(n1) * int(n2))

print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>Multiplication Of Numbers </title>";
print "</head>";
print "<body>";

#to print out the total variable I have to convert the integer value into a string variable.
print "<p>total= " + str(total) + "</p>";
print "</body>";
print "</html>";

Here is a working example:
http://pythoninhtmlexamples.com/files/simple_math.py

PS. if your file constantly gives you an error, and your syntax is totally correct, and you are about to throw the computer across the room in frustration 🙂 , make sure you set your file permissions to 755.

Facebooktwitterredditpinterestlinkedinmail
Tags: , , , ,
Previous Post

How To Simply Connect Python To MySQL On Your Site

Next Post

How To Insert Python In Your HTML Page