import re def comma_me(amount): orig = amount new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', amount) if orig == new: return new else: return comma_me(new) f = 12345678 print comma_me(`f`) Output: 12,345,678
import re def comma_me(amount): orig = amount new = re.sub("^(-?\d+)(\d{3})", '\g<1>,\g<2>', amount) if orig == new: return new else: return comma_me(new) f = 12345678 print comma_me(`f`) Output: 12,345,678