Insert comma in number?

John Posner jjposner at optimum.net
Thu Mar 7 20:06:03 CET 2013


Peter Otten wrote:

> Last not least there's the option to employ locale-aware formatting:


Not quite last ... there's the mind-bending regular expression route:

   import re
   re.sub(r"(?<=\d)(?=(\d\d\d)+$)", ",", "12345678")   # 12,345,678
   re.sub(r"(?<=\d)(?=(\d\d\d)+$)", ",", "-54321")     # -54,321

-John






More information about the Python-list mailing list