Can anyone improve my code for efficiency or suggest any other fast way than mine | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone improve my code for efficiency or suggest any other fast way than mine

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". https://code.sololearn.com/cn1Q8cwt0qIN/?ref=app

25th Sep 2021, 8:51 AM
Prabhas Koya
2 Answers
+ 4
# Here are some ways to do it with formatting num = 123456789.4598 s = f'{num:,}' # F-Strings s = '{:,}'.format(num) # String Formatting s = format(num, ',') # Built-in Format Function print(s)
25th Sep 2021, 9:16 AM
SoloProg
SoloProg - avatar
+ 4
num = 123456789.45 print(f"{num:,}") # Keep learning & happy coding :D
25th Sep 2021, 8:58 AM
SoloProg
SoloProg - avatar