<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Tue, May 2, 2017 at 4:31 AM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Rather than duplicate the API and logic everywhere, I suggest we add a<br>
new string method. My suggestion is str.chunk(size, delimiter=' ') and<br>
str.rchunk() with the same arguments:<br>
<br>
"1234ABCDEF".chunk(4)<br>
=> returns "1234 ABCD EF"<br>
<br>
rchunk will be useful for money or other situations where we group from<br>
the right rather than from the left:<br>
<br>
"$" + str(10**6).rchunk(3, ',')<br>
=> returns "$1,000,000"<br><br>
# Format mobile phone number in the Australian style<br>
"04123456".rchunk((4, 3))<br>
=> returns "0412 345 678"<br>
<br>
# Format an integer in the Indian style<br>
str(123456789).rchunk((3, 2), ",")<br>
=> returns "12,34,56,789"<br></blockquote><div><br></div><div>I like this general idea very much.  Dealing with lakh and crore is a very nice feature (and one that the `.format()` mini-language sadly fails to handle; it assumes numeric delimiters can only be commas, and only ever three positions).</div><div><br></div><div>But I'm not sure the semantics you propose is flexible enough.  I take it that the tuple means (<first-delimiter>, <other-delimiters>) from your examples.  But I don't think that suffices for every common format.  It would be fine to get a USA phone number like:</div><div><br></div><div>    str(4135559414).rchunk((4,3),'-')  # -> 413-555-9414</div><div><br></div><div>But for example, looking somewhat at random at an international call (<a href="https://en.wikipedia.org/wiki/Telephone_numbers_in_Belgium">https://en.wikipedia.org/wiki/Telephone_numbers_in_Belgium</a>)</div><div><pre style="font-family:monospace,courier;color:rgb(0,0,0);background-color:rgb(248,249,250);border:1px solid rgb(234,236,240);padding:1em;white-space:pre-wrap;line-height:1.3em;font-size:13.125px"><b>Dialing from New York to Brussel</b>
<b>011-32-2-555-12-12</b> - Omitting the leading "0".</pre></div><div>Maybe your API is for any length tuple, with the final element repeated.  So I guess maybe this example could be:</div><div><br></div><div>    "0113225551212".rchunk((2,2,3,1,2,3),'-')</div><div> </div></div>I don't care about this method being called .chunk() vs. .delimit() vs. something else.<br clear="all"><div><br></div>-- <br><div class="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>