Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 333289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:56:50+00:00 2026-05-12T09:56:50+00:00

I haven’t been able to find a good example of subclassing string.Template in Python,

  • 0

I haven’t been able to find a good example of subclassing string.Template in Python, even though I’ve seen multiple references to doing so in documentation.

Are there any examples of this on the web?

I want to change the $ to be a different character and maybe change the regex for identifiers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:56:51+00:00Added an answer on May 12, 2026 at 9:56 am

    From python docs:

    Advanced usage: you can derive
    subclasses of Template to customize
    the placeholder syntax, delimiter
    character, or the entire regular
    expression used to parse template
    strings. To do this, you can override
    these class attributes:

    • delimiter – This is the literal string describing a placeholder
      introducing delimiter. The default
      value $. Note that this should not be
      a regular expression, as the
      implementation will call re.escape()
      on this string as needed.

    • idpattern – This is the regular expression describing the pattern for
      non-braced placeholders (the braces
      will be added automatically as
      appropriate). The default value is the
      regular expression [_a-z][_a-z0-9]*.

    Example:

    from string import Template
    
    class MyTemplate(Template):
        delimiter = '#'
        idpattern = r'[a-z][_a-z0-9]*'
    
    >>> s = MyTemplate('#who likes $what')
    >>> s.substitute(who='tim', what='kung pao')
    'tim likes $what'
    

    In python 3:

    New in version 3.2.

    Alternatively, you can provide the entire regular expression pattern
    by overriding the class attribute pattern. If you do this, the value
    must be a regular expression object with four named capturing groups.
    The capturing groups correspond to the rules given above, along with
    the invalid placeholder rule:

    • escaped – This group matches the escape sequence, e.g. $$, in the default pattern.
    • named – This group matches the unbraced placeholder name; it should not include the delimiter in capturing group.
    • braced – This group matches the brace enclosed placeholder name; it should not include either the delimiter or braces in the capturing
      group.
    • invalid – This group matches any other delimiter pattern (usually a single delimiter), and it should appear last in the regular
      expression.

    Example:

    from string import Template
    import re
    
    class TemplateClone(Template):
        delimiter = '$'
        pattern = r'''
        \$(?:
          (?P<escaped>\$) |   # Escape sequence of two delimiters
          (?P<named>[_a-z][_a-z0-9]*)      |   # delimiter and a Python identifier
          {(?P<braced>[_a-z][_a-z0-9]*)}   |   # delimiter and a braced identifier
          (?P<invalid>)              # Other ill-formed delimiter exprs
        )
        '''
    
    class TemplateAlternative(Template):
        delimiter = '[-'
        pattern = r'''
        \[-(?:
           (?P<escaped>-) |            # Expression [-- will become [-
           (?P<named>[^\[\]\n-]+)-\] | # -, [, ], and \n can't be used in names
           \b\B(?P<braced>) |          # Braced names disabled
           (?P<invalid>)               #
        )
        '''
    
    >>> t = TemplateClone("$hi sir")
    >>> t.substitute({"hi": "hello"})
    'hello sir'
    
    >>> ta = TemplateAlternative("[-hi-] sir")
    >>> ta.substitute({"hi": "have a nice day"})
    'have a nice day sir'
    >>> ta = TemplateAlternative("[--[-hi-]-]")
    >>> ta.substitute({"hi": "have a nice day"})
    '[-have a nice day-]'
    

    Apparently it is also possible to just omit any of the regex groups escaped, named, braced or invalid to disable it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 171k
  • Answers 171k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Seems to me that a good example of where a… May 12, 2026 at 2:21 pm
  • Editorial Team
    Editorial Team added an answer You'll have to search the script for the imports you… May 12, 2026 at 2:21 pm
  • Editorial Team
    Editorial Team added an answer Here's how I'd do it. The phrase to be translated… May 12, 2026 at 2:21 pm

Related Questions

I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS
I have a JSP page retrieving data and when single or double quotes are
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.