def format_tag(template, tag_name, attrs):
    """
    Format a template of a tag with attributes, so that:
    >>> format_tag('<{} {}>', 'span', {'id': 'foo'})
    >>> '<span id="foo">'
    Note: use `klass` instead of the `class` attribute.
    """
    return template.format(tag_name, ' '.join(['{}="{}"'.format(key.replace('klass', 'class'), val) 
                                               for (key, val) in attrs.iteritems()]))