[Python-Dev] Locale-specific formatting

MRAB python at mrabarnett.plus.com
Sat Dec 18 01:08:47 CET 2010

I had a thought about locale-specific formatting.

Currently, when we want to do locale-specific formatting we use the
locale module like this:

 >>> locale.format("%d", 12345, grouping=False)
 >>> locale.format("%d", 12345, grouping=True)

This makes it harder to use more than one locale at a time, or one
which is different from the default.

My thought was that we could specify a locale in the format
specification mini-language and the parameter list of str.format,
something like this:

 >>> loc = locale.getlocale()
 >>> "{0:@1}".format(12345, loc)
 >>> "{0:, at 1}".format(12345, loc)
 >>> "UK says {value:,.1f at uk} and France says 
{value:,.1f at france}".format(value=12345, uk=uk_loc, france=france_loc)
'UK says 1,234.5 and France says 1 234,5'


More information about the Python-Dev mailing list