Add Commas to String in Python

A string is capable of holding both numeric characters and letters of the alphabet. There may sometimes be a need to add commas to relevant spaces in a string containing numerical values in order to represent the number according to the specified currency system.

This tutorial focuses on and demonstrates the different ways available to add commas to a string in Python.

The article has been divided into two parts, with the latter half focusing on adding commas to a number in a manner that fits an appropriate currency system while the first half simply deals with strings normally and adds a comma after every character.

How To Add Commas to String in Python?

In this part of the article, we will tackle actual strings and how to add commas after each character of a string in Python.

Using the join() function.

The join() function is capable of concatenating a new string with a comma added after every character. This process can be done seamlessly in just a single line of code.
Both the string to be worked upon and the value to be inserted, which is a comma in our case, are passed as arguments to this function.
The following code uses the join() function to add commas to a string in Python.

Output:

j,2,b,e,x,a,m,p,l,e

Using a for loop.

A for loop can be utilized to manually iterate through each character of the given string and add a comma after every single one of the characters.
This approach also makes use of the in-built strip() function that Python provides.
The following code uses a for loop to add commas to a string in Python.

Output:

‘j,2,b,e,x,a,m,p,l,e,’

Using the replace() function.

The replace() function carries out the basic task of replacing one specified phrase with another. It takes both these phrases as its arguments.
The following code uses the replace() function to add commas to a string in Python.

Output:

j,2,b,e,x,a,m,p,l,e

How To Add Commas to String in Python to Represent Numbers in A Currency?

There are several ways of implementing the task of adding commas to string in Python. From making use of string formatting to iterating with a for loop, all the approaches that lead to an accurate result have been mentioned in the article below.
For this, we will first take a number as an integer and then represent it as a string that holds the same number with commas added to it as per the currency system.

Using the format() function.

The original format() function is the oldest method in the book to implement string handling or string formatting in Python. The string to be operated on is passed as an argument to this function along with a format specifier to implement the task at hand.
The following code uses the format() function to add commas to a string in Python to represent numbers in a currency.

Output:

800,000,000

Here, we should note that the appropriate format specifier needs to be utilized in order to carry out this task successfully. In our case, the appropriate format specifier is d, which represents that a decimal value is initially taken and stored.

Using the str.format() function.

The str.format() function is another way to implement string formatting in Python. It makes use of curly braces to define placeholders after which the str.format() function is called.
The following code uses the str.format() function to add commas to a string in Python to represent numbers in a currency.

Output:

800,000,000

Using f-strings.

The concept of f-strings has been massively popular since its introduction in Python 3.6 and it is yet another way to implement string formatting in Python.

F-strings tend to be faster in operation than their other two peers and is able to perform every task that the other two approaches can perform, if not more.

F-strings make use of the letter f as a prefix to the given string for its implementation.

The following code uses f-strings to add commas to a string in Python to represent numbers in a currency.

Output:

800,000,000

Using the locale module.

We can make use of the locale module functions to implement the task of displaying numbers with appropriate commas as per the currency system.
The locale module provides several functions that exclusively deal with numbers and currencies.
The following code uses the locale module to add commas to a string in Python to represent numbers in a currency.

Output:

800,000,000.00

Conclusion.

This tutorial demonstrates all the different ways available to add commas to a string in Python. This article takes two different approaches into account and provides a solution to both problems.

The article is carefully split into two parts, with the latter half providing an in-depth solution to adding commas to a number in a manner that fits an appropriate currency system while the first half simply deals with strings normally and adds a comma after every character.

Depending on the user’s requirements, they can find a solution that fits them best from the set provided and explained in this article.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *