Convert string with comma as thousands separator to Number in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Convert string with comma as thousands separator to Number in Python #

To convert a string with a comma as the thousands separator to a number:

  1. Use the locale.setlocale() method to set the locale to en_US.UTF-8.
  2. Use the locale.atoi() method to convert the string to a number for integers.
  3. Use the locale.atof() method to convert the string to a number for floats.
main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # ✅ convert string with comma thousands separator to integer my_str = '123,456,789' result = locale.atoi(my_str) print(result) # 👉️ 123456789 print(type(result)) # 👉️ <class 'int'> # ----------------------------------------------------- # ✅ convert string with comma thousands separator to float my_str = '123,456.7891' result = locale.atof(my_str) print(result) # 👉️ 123456.7891 print(type(result)) # 👉️ <class 'float'> # ----------------------------------------------------- # ✅ convert string with comma thousands separator (str.replace()) my_str = '123,456,789' result = int(my_str.replace(',', '')) print(result) # 👉️ 123456789

We used the locale.setlocale() method to set the locale to en_US.UTF-8.

main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

The setlocale() method takes the category and the locale.

The locale.LC_ALL category sets the locale for all categories.

The thousands separator character in the United States is a comma, so that's what we set the locale parameter to.

The locale.atoi method takes a string and converts it to an integer.

main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_str = '123,456,789' result = locale.atoi(my_str) print(result) # 👉️ 123456789 print(type(result)) # 👉️ <class 'int'>

The locale.atof method takes a string and converts it to a floating-point number.

main.py
import locale locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') my_str = '123,456.7891' result = locale.atof(my_str) print(result) # 👉️ 123456.7891 print(type(result)) # 👉️ <class 'float'>
Make sure to use the setlocale() method before calling the locale.atoi() or locale.atof() methods.

Alternatively, you can use the str.replace() method.

To convert a string with a comma as the thousands separator to a number:

  1. Use the str.replace() method to remove the commas from the string.
  2. Use the int() or float() classes to convert the string to a number.
main.py
my_str = '123,456,789' result = int(my_str.replace(',', '')) print(result) # 👉️ 123456789 my_str = '123,456.7891' result = float(my_str.replace(',', '')) print(result) # 👉️ 123456.7891 print(type(result)) # 👉️ <class 'float'>

We first used the str.replace() method to remove the commas from the string.

main.py
my_str = '123,456,789' print(my_str.replace(',', '')) # 👉️ '123456789'

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

Once the commas are removed, we can use the int() or float() classes to convert the string to a number.

Which approach you pick is a matter of personal preference. I'd go with the str.replace() method because it gets the job done and is quite intuitive.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.