how can i split numbers thee to three in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

how can i split numbers thee to three in python

for example 1235633 1,235,633

7th Dec 2018, 1:54 PM
omid Reza Saberi Ansari
omid Reza Saberi Ansari - avatar
5 Answers
+ 4
Or: print('{:,}'.format(1000000))
7th Dec 2018, 2:55 PM
HonFu
HonFu - avatar
+ 8
Found these in a SO thread, more can be found in there, I just gathered some for my own curiosity, because I found number formatting is interesting, and important ; ) inum = 1235633 fnum = 1235633.123 print("{:,}".format(inum)) # int print("{:,}".format(fnum)) # float print(format(inum, ',d')) # int print(format(fnum, ',.3f')) # float print(f"{inum:,d}") # int print(f"{fnum:,.3f}") # float # Source: # https://stackoverflow.com/questions/1823058/how-to-print-number-with-commas-as-thousands-separators Hth, cmiiw
7th Dec 2018, 3:04 PM
Ipang
+ 4
Idea: Convert it into a string. Then make a list by slicing it into chunks of three (from the right). Finally, join the elements of the list with commas. Try to implement it now. If you get stuck, show your attempt, and we'd be happy to help you further.
7th Dec 2018, 2:46 PM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Isn't our beloved Python always? ;-)
7th Dec 2018, 3:02 PM
HonFu
HonFu - avatar
+ 1
Cool! That's convenient! 😊
7th Dec 2018, 2:59 PM
Kishalaya Saha
Kishalaya Saha - avatar