1. Home
  2. Computing & Technology
  3. Python

Creating HTML and XHTML Pages in Python - With and Without CGI

From , former About.com Guide

2 of 6

Creating HTML Output

First, creating a new HTML document is simply a matter of putting the relevant HTML as the argument of a print command:

print '''
content-type: text/html

<html>
<head>
<title> This is the title </title>
</head>
<body>
This is the body.
</body>
</html>
'''

Recall that triple quotes in Python tell it to print what follows verbatim. It disregards any character sequences for strings, etc. If you need to represent variable data, default to using the standard single quotation mark method.

x = "variable"
print "This is a %s." %(x)

With this information, one can create a bevy of HTML pages with variable content very quickly and automagically.

Explore Python
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Web Development
  5. Python and HTML - Creating HTML and XHTML Pages in Python - Creating HTML Output

©2009 About.com, a part of The New York Times Company.

All rights reserved.