Creating formatted output using picture strings

Stephen Hansen apt.shansen at gmail.com
Wed Feb 10 00:24:20 EST 2010


On Tue, Feb 9, 2010 at 8:45 PM, <python at bdurham.com> wrote:

> Does Python provide a way to format a string according to a 'picture'
> format?
>
> For example, if I have a string '123456789' and want it formatted like
> '(123)-45-(678)[9]', is there a module or function that will allow me to do
> this or do I need to code this type of transformation myself?
>

Although I usually don't jump to suggesting regular expressions, its the
easiest way I can figure out to do this.

>>> import re
>>> >>> print re.sub(r"(\d{3})(\d{2})(\d{3})(\d)", r"(\1)-\2-(\3)[\4]",
"123456789")
(123)-45-(678)[9]

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100209/9bdef633/attachment-0001.html>


More information about the Python-list mailing list