HOWTO · Python

How to Remove Commas From String in Python

To remove comma from a string in Python, we can use the str.replace() method and the re package.

On this page

To remove commas from a Python string, call str.replace() with a comma as the old substring and an empty string as the replacement. The call returns a new string; it does not change the original because Python strings are immutable. The examples below show the returned values directly.

Run this example:

text = "Delft, Stack, Netherlands"
cleaned = text.replace(",", "")
print(cleaned)
Delft Stack Netherlands

Assigning the result to cleaned keeps the original text available if later code still needs the commas. You can also pass the result directly to another function or assign it back to the same variable name; either way, replace() creates and returns a string value.

Remove all commas from a string with str.replace()

str.replace(old, new[, count]) searches for occurrences of the exact substring old and returns a string with matching occurrences replaced by new. In this task, old is "," and new is "". With no count argument, every exact comma match is replaced, including commas at the beginning or end of the string and consecutive commas. Other characters remain in their original order.

The call works on the string value itself, so use it as text.replace(...) rather than calling it on the str type with an input string. A string that contains no comma is returned unchanged in value. The operation does not trim spaces around words, remove periods, or normalize a comma-like character that is different from the ordinary comma; it only matches the substring you provide.

Run this example:

text = "Delft, Stack, Netherlands"
cleaned = text.replace(",", "")
print("Original:", text)
print("Without commas:", cleaned)
Original: Delft, Stack, Netherlands
Without commas: Delft Stack Netherlands

The old argument tells Python what to find, and new tells it what to put in each matching position. Using an empty replacement deletes each match while retaining the surrounding text. The result for the sample has the words separated by their existing spaces. Keep those spaces when that is the desired formatting; stripping or collapsing whitespace would be a separate transformation.

Remove only the first comma with str.replace()

To remove just the first matching comma, provide 1 as the third argument: text.replace(",", "", 1). This optional count parameter limits how many replacements the method performs, not how many commas the input may contain. The search proceeds from left to right, so a later comma remains in the returned string. If there is no matching comma, the string’s value remains unchanged.

Run this example:

text = "Delft, Stack, Netherlands"
cleaned = text.replace(",", "", 1)
print(cleaned)
Delft Stack, Netherlands

Choose this form when the first separator has a special role and later separators must be preserved. For example, replacing once can remove a leading label delimiter without flattening the rest of a comma-separated value. If the count is omitted, str.replace() uses its all-matches behavior; setting it to 1 is what changes the result. The input remains available as text after the call, since strings are immutable.

Remove commas with re.sub()

For the same literal comma removal, Python’s re.sub() function is another option. It accepts a pattern, a replacement, and the input string in that order: re.sub(pattern, replacement, string). The comma pattern matches the comma character, while the empty replacement removes each match. Since re.sub() is a function in the re module, import the module before calling it.

Run this example:

import re

text = "Delft, Stack, Netherlands"
cleaned = re.sub(",", "", text)
print(cleaned)
Delft Stack Netherlands

This pattern is literal and needs no regular-expression feature beyond matching a comma, so str.replace() is usually the simpler choice for this task. re.sub() is useful when a pattern is already part of a larger regular-expression operation. Its default behavior replaces all matches, and its returned string leaves the original input unchanged. If the pattern finds no match, the returned value is unchanged in content. Use the exact pattern that describes the characters you intend to remove; a plain comma pattern does not also remove other punctuation or visually similar characters.