Formatting numbers in python for display -
i trying output longer integer in more readable manner. how can format string spaces after number of digits?
for example, 12345678
output 1234 56 78
one option use re
module, match number regex , reformat it:
import re re.sub(r"(\d{4})(\d{2})(\d{2})", r"\1 \2 \3", "12345678") # '1234 56 78'
or readability might want format thousand separator:
"{:,d}".format(12345678) # '12,345,678'
Comments
Post a Comment