Python | Add comma between numbers
Sometimes, while working with currencies, we need to put commas between numbers to represent a currency, hence given a string, we can have a problem to insert commas into them. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using str.format() The string format function can be used by supplying the valid formatter to perform the formatting. The string formatter is called with the value as argument to perform this particular task.
Python3
# Python3 code to demonstrate working of # Adding comma between numbers # Using str.format() # initializing number test_num = 1234567 # printing original number print ( "The original number is : " + str (test_num)) # Using str.format() # Adding comma between numbers res = ( '{:,}' . format (test_num)) # printing result print ( "The number after inserting commas : " + str (res)) |
The original number is : 1234567 The number after inserting commas : 1,234,567
Method #2 : Using format() This task can also be performed without taking the help of format library of strings but the normal format library that can use the “d” notation of numbers and simply insert the commas where required.
Python3
# Python3 code to demonstrate working of # Adding comma between numbers # Using format() # initializing number test_num = 1234567 # printing original number print ( "The original number is : " + str (test_num)) # Using format() # Adding comma between numbers res = ( format (test_num, ',d' )) # printing result print ( "The number after inserting commas : " + str (res)) |
The original number is : 1234567 The number after inserting commas : 1,234,567
Method #3: Using re.sub()
The re.sub() method can be used along with regular expressions to match the required pattern of digits and insert commas in between them.
Python3
# Python3 code to demonstrate working of # Adding comma between numbers # Using re.sub() import re # initializing number test_num = 1234567 # printing original number print ( "The original number is : " + str (test_num)) # Using re.sub() # Adding comma between numbers res = re.sub(r '(\d{3})(?=\d)' , r '\1,' , str (test_num)[:: - 1 ])[:: - 1 ] # printing result print ( "The number after inserting commas : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original number is : 1234567 The number after inserting commas : 1,234,567
This method will match every 3 digits in the number, and insert a comma before it using a positive lookahead. The string is first reversed using slicing so that the commas are inserted from right to left, and then reversed back to get the final output.
Time Complexity and Auxiliary space of this approach is O(n)
Method#4: Using f-string
Python3
test_num = 1234567 # printing original number print ( "The original number is : " + str (test_num)) res = f "{test_num:,}" # printing result print ( "The number after inserting commas : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original number is : 1234567 The number after inserting commas : 321,654,7
Time Complexity: O(n)
Please Login to comment...