← Back to challenges

Format Number with Comma(s) Separating Thousands

PythonHardformattingnumbersstringsregex

Instructions

Create a function that takes a number as an argument and returns a string formatted to separate thousands.

Examples

format_num(1000) ➞ "1,000"

format_num(100000) ➞ "100,000"

format_num(20) ➞ "20"

Notes

You can expect a valid number for all test cases.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Smooth Sentences