On Tue, May 2, 2017 at 4:31 AM, Steven D'Aprano <st...@pearwood.info> wrote:
> Rather than duplicate the API and logic everywhere, I suggest we add a > new string method. My suggestion is str.chunk(size, delimiter=' ') and > str.rchunk() with the same arguments: > > "1234ABCDEF".chunk(4) > => returns "1234 ABCD EF" > > rchunk will be useful for money or other situations where we group from > the right rather than from the left: > > "$" + str(10**6).rchunk(3, ',') > => returns "$1,000,000" > > # Format mobile phone number in the Australian style > "04123456".rchunk((4, 3)) > => returns "0412 345 678" > > # Format an integer in the Indian style > str(123456789).rchunk((3, 2), ",") > => returns "12,34,56,789" > 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). 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: str(4135559414).rchunk((4,3),'-') # -> 413-555-9414 But for example, looking somewhat at random at an international call ( https://en.wikipedia.org/wiki/Telephone_numbers_in_Belgium) *Dialing from New York to Brussel**011-32-2-555-12-12* - Omitting the leading "0". Maybe your API is for any length tuple, with the final element repeated. So I guess maybe this example could be: "0113225551212".rchunk((2,2,3,1,2,3),'-') I don't care about this method being called .chunk() vs. .delimit() vs. something else. -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/