Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8033
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail |
|---|---|
| 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> |
| X-Public-Key-ID | 0xAC128405 |
| X-Public-Key-Fingerprint | 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-pubkey.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| Date | Tue, 21 Jun 2011 08:19:09 +1000 |
| Message-ID | <87pqm8qh7m.fsf@benfinney.id.au> (permalink) |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) |
| Cancel-Lock | sha1:ybWu31lUuf8NZISDtkh9QjfWlJE= |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| Lines | 38 |
| Organization | Unlimited download news at news.astraweb.com |
| NNTP-Posting-Host | c7acb48f.news.astraweb.com |
| X-Trace | DXC=0jP04e5]cePeIcSKiG^o_UL?0kYOcDh@ZSBc;\8ijUd[QS[jEnCJZXQUGD@iN[Hd?_Wjn7j?6O1fX |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:8033 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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