On Tue, Feb 9, 2010 at 8:45 PM,  <span dir="ltr"><<a href="mailto:python@bdurham.com">python@bdurham.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">


<div><div style="font-family:Arial;font-size:medium" dir="ltr"><div>Does Python provide a way to format a string according to a 'picture' format?</div>
<div> </div>
<div>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?</div>

</div></div></blockquote><div> </div><div>Although I usually don't jump to suggesting regular expressions, its the easiest way I can figure out to do this.</div><div><br></div><div>>>> import re</div><div>>>> >>> print re.sub(r"(\d{3})(\d{2})(\d{3})(\d)", r"(\1)-\2-(\3)[\4]", "123456789")</div>

<div>(123)-45-(678)[9]</div><div><br></div></div><div name="mailplane_signature">--S</div>