• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Strings / Remove Commas From String in Python

Remove Commas From String in Python

Author: Aditya Raj
Last Updated: February 8, 2022

In python, we use strings to analyze text data. We need to preprocess the text data before analysis. Sometimes, we might need to remove characters like commas or apostrophes from the text. In this article, we will discuss how we can remove commas from a given string in python.

Remove commas from string using for loop

The simplest way to remove a comma from a string is to create a new string leaving the comma behind. 

In this approach, we will first create an empty string newString. After that, we will traverse through the input string character by character using a for loop. While traversing, we will check if the current character is a comma or not. If the character is not a comma, we will add the present character to newString using the string concatenation operation. Once the execution of the for loop finishes, we will get the output string free from comma character in the variable newString.

You can observe this entire process in the following example.

myString = "This, is, a, string, that, has, commas, in, it."
print("Original String:", myString)
newString = ""
for character in myString:
    if character != ",":
        newString = newString + character
print("Output String:", newString)

Output:

Original String: This, is, a, string, that, has, commas, in, it.
Output String: This is a string that has commas in it.

Using the replace() method

Instead of traversing through the entire string to remove commas from the string, we can use the replace() method to remove commas from any given string. 

The syntax of the replace method is as follows.

str.replace(old_character, new_character, count)

Here,

  • The parameter old_character is used to pass the character to the replace method that needs to be removed. We will remove the comma “,” as the first input argument.
  • The parameter new_character is used to pass the character that will replace the old_character. We will pass an empty string as the new_character so that the comma character gets removed.
  • The parameter count is used to decide how many instances of old_character will be replaced by the new_character in the string. It is an optional parameter and we can leave it empty. In such a case, all the instances of old_character in the string will be replaced with new_character.
  • The replace() method returns the modified string after replacing all the instances of old_character with new_character.

We can use the str.replace() method to remove commas from a given string as follows.

myString = "This, is, a, string, that, has, commas, in, it."
print("Original String:", myString)
newString = myString.replace(",", "")
print("Output String:", newString)

Output:

Original String: This, is, a, string, that, has, commas, in, it.
Output String: This is a string that has commas in it.

Remove commas from string using re.sub() method

Regular expressions are a great tool to manipulate strings. We can also use them to remove commas from a string in python. For this, we will use the re.sub() method.

The syntax for re.sub() method is as follows.

re.sub(old_character, new_character, input_string)

Here,

  • The parameter old_character is used to pass the character to the re.sub() method that needs to be removed. We will remove the comma “,” as the first input argument.
  • The parameter new_character is used to pass the character that will replace the old_character. We will pass an empty string as the new_character so that the comma character gets removed.
  • The input_string parameter is used to pass the string that needs to be modified to the re.sub() method .
  • After execution, the method returns the modified string.

We can use the re.sub() method to remove commas from a given string as follows.

import re

myString = "This, is, a, string, that, has, commas, in, it."
print("Original String:", myString)
newString = re.sub(",", "", myString)
print("Output String:", newString)

Output:

Original String: This, is, a, string, that, has, commas, in, it.
Output String: This is a string that has commas in it.

Conclusion

In this article, we have discussed three ways to remove commas from a string in python.  To know more about strings in python, you can read this article on regular expressions in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Strings Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us