cast variable | Python In HTML Examples https://pythoninhtmlexamples.com Showing You How You Can Use Python On Your Website Fri, 05 Jul 2019 17:39:44 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 194754043 Simple Python Variables Examples https://pythoninhtmlexamples.com/simple-python-variables-examples/?utm_source=rss&utm_medium=rss&utm_campaign=simple-python-variables-examples Fri, 05 Jul 2019 17:36:21 +0000 http://pythoninhtmlexamples.com/?p=49 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 […]

The post Simple Python Variables Examples first appeared on Python In HTML 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.

FacebooktwitterredditpinterestlinkedinmailThe post Simple Python Variables Examples first appeared on Python In HTML Examples.]]>
49