Thousands separator
Write a function named format_number
that takes a non-negative number as its only parameter.
Your function should convert the number to a string and add commas as a thousands separator.
For example, calling format_number(1000000)
should return "1,000,000"
.
Â
==== SOLUTION ====
You can implement the `format_number` function in Python by using the built-in `format()` function to add the thousands separator. Here’s how you can do it:
“`python
def format_number(number):
        return format(number, ‘,’)
# Test the function
print(format_number(1000000)) # Output: “1,000,000”
“`
Explanation:
1. `def format_number(number):`: This line defines a function named `format_number` that takes a single parameter `number`, which is a non-negative number.
2. `return format(number, ‘,’)`: This line uses the `format()` function to format the number `number` with a thousands separator. The `’,’` argument specifies that a comma should be used as the separator.
This implementation correctly converts the non-negative number to a string and adds commas as a thousands separator.
Â