'Returns a string or Unicode representation of this tag and
its contents. To get Unicode, pass None for encoding.
NOTE: since Python\'s HTML parser consumes whitespace, this
method is not certain to reproduce the whitespace present in
the original string.'

def __str__(self, encoding=DEFAULT_OUTPUT_ENCODING, prettyPrint=False, indentLevel=0):

    encodedName = self.toEncoding(self.name, encoding)
    attrs = []
    if self.attrs:
        for (key, val) in self.attrs:
            fmt = '%s="%s"'
            if isString(val):
                if (self.containsSubstitutions and ('%SOUP-ENCODING%' in val)):
                    val = self.substituteEncoding(val, encoding)
                if ('"' in val):
                    if ("'" in val):
                        val = val.replace('"', '&quot;')
                    else:
                        fmt = "%s='%s'"
                if self.convertHTMLEntities:
                    val = re.sub('&(\\w+);', self._convertEntities, val)
                val = val.replace('<', '&lt;').replace('>', '&gt;')
                val = self.BARE_AMPERSAND.sub('&amp;', val)
            attrs.append((fmt % (self.toEncoding(key, encoding), self.toEncoding(val, encoding))))
    close = ''
    closeTag = ''
    if self.isSelfClosing:
        close = '    /'
    else:
        closeTag = ('</%s>' % encodedName)
    (indentTag, indentContents) = (0, 0)
    if prettyPrint:
        indentTag = indentLevel
        space = ('    ' * (indentTag - 1))
        indentContents = (indentTag + 1)
    contents = self.renderContents(encoding, prettyPrint, indentContents)
    if self.hidden:
        s = contents
    else:
        s = []
        attributeString = ''
        if attrs:
            attributeString = ('    ' + '    '.join(attrs))
        if prettyPrint:
            s.append(space)
        s.append(('<%s%s%s>' % (encodedName, attributeString, close)))
        if prettyPrint:
            s.append('\n')
        s.append(contents)
        if (prettyPrint and contents and (contents[(-1)] != '\n')):
            s.append('\n')
        if (prettyPrint and closeTag):
            s.append(space)
        s.append(closeTag)
        if (prettyPrint and closeTag and self.nextSibling):
            s.append('\n')
        s = ''.join(s)
    return s
