Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8033

Re: What is this syntax ?

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: What is this syntax ?
References <mailman.178.1308554725.1164.python-list@python.org>
Date 2011-06-21 08:19 +1000
Message-ID <87pqm8qh7m.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


Claudiu Popa <cpopa@bitdefender.com> writes:

> Hello,

(Please don't top-post. Instead, interleave your responses below each
quoted part you're responding to, as in this message. See also
<https://secure.wikimedia.org/wikipedia/en/wiki/Posting_style#Interleaved_style>.)

> Isn't this similar to php interpolation? And quite readable imo.
>
> >>> import string
> >>> template = string.Template("$scheme://$host:$port/$route#$fragment")
> >>> template.substitute(scheme="http", host="google.com", port="80", route="", fragment="")
> 'http://google.com:80/#'
> >>>

This style is so useful that a very similar system was proposed, and
accepted, in PEP 3101 as a method of the built-in types. It makes most
uses of the ‘string.Template’ class obsolete.

The text types (‘str’, ‘unicode’) now have a very similar capability as
part of the type. Works in Python 2.6 or later, and Python 3 or later.

    >>> template = "{scheme}://{host}:{port}/{route}#{fragment}"
    >>> template.format(scheme="http", host="google.com", port="80", route="", fragment="")
    'http://google.com:80/#'

“This method of string formatting is the new standard in Python 3.0, and
should be preferred to the % formatting described in String Formatting
Operations in new code.”

<URL:http://docs.python.org/library/stdtypes.html#str.format>

-- 
 \                “Room service? Send up a larger room.” —Groucho Marx |
  `\                                                                   |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: What is this syntax ? Claudiu Popa <cpopa@bitdefender.com> - 2011-06-20 10:18 +0300
  Re: What is this syntax ? Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 08:19 +1000
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-20 19:46 -0500
    Re: new string-formatting preferred? (was "What is this syntax ?") Terry Reedy <tjreedy@udel.edu> - 2011-06-20 22:17 -0400
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-21 06:33 -0500
    Re: new string-formatting preferred? (was "What is this syntax ?") Terry Reedy <tjreedy@udel.edu> - 2011-06-21 18:19 -0400
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-21 18:02 -0500

csiph-web