HomeLinuxTake away Commas From String Python

Take away Commas From String Python


Python is a useful programming language that facilitates customers with a number of built-in capabilities and operators to carry out completely different operations, akin to eliminating additional commas, characters, or any symbols from the string. Typically programmers need to take away the additional commas from any string that may be added beforehand, and now they don’t need to preserve it within the code or string.

This put up will present the completely different strategies for eradicating commas from the string in Python.

How one can Take away Commas From String in Python?

The next strategies are used to take away commas from a string in Python:

Technique 1: Take away Commas From String in Python Utilizing “for” Loop

To take away the commas from the string, the “for” loop can be utilized. The “for” loop is the iterative perform in Python that may be utilized for iterating over a sequence. The offered information generally is a tuple, a set of directions, an inventory, or a string.

Instance

First, create a string variable:

original_string = “This, is, my testing, code”

 

Then, create yet another empty variable that’s used for storing the results of executed string which comprises no commas:

 

Now, we’ll traverse by means of the desired string character by character by using a “for” loop. In the course of the traversing, we’ll test by means of the “if” assertion whether or not our present character is a comma or not. If the character will not be a comma, then add the present character to the beforehand created empty string by using the string concatenation “+” operation. In any other case, the checked character will take away from the string:

for alphabet in original_string:
  if alphabet != “,”:
    resultant_string = resultant_string + alphabet

 

As soon as the loop execution and character matches end, we’ll get the output by means of the “print()” assertion:

 

Output

Technique 2: Take away Commas From String in Python Utilizing “re.sub()” Technique

The opposite approach to remove the commas from the string in Python is by using the “regex()” or “re” methodology. The “re” time period is an everyday expression that’s the Python’s built-in methodology used to take care of systematic expressions. This bundle comprises the “re.sub()” perform that performs substitute, swapping, or eliminating the commas from the strings in python.

Syntax

The syntax of the “re.sub()” perform is acknowledged under:

re.sub(oldValue, replaceValue, input_string)

 

Right here:

  • oldValue” argument is used to cross the previous worth to the “sub()” perform that must be eradicated.
  • replaceValue” parameter is the previous worth that may substitute the “oldValue”.
  • input_string” argument is the string that requires to be modified to the “sub()” perform.

Let’s take a look at the next instance for higher understanding!

Instance

First, import the regex “re” bundle

 

Then, name the “re.sub()” methodology with the required arguments and cross them to the “resultant_string

resultant_string = re.sub(“,”, “”, original_string)

 

Lastly, use the “print()” perform and get the commas eradicated modified string:

 

It may be seen within the offered output, the resultant string has no commas:

Technique 3: Take away Commas From String in Python Utilizing “substitute()” Technique

To remove the commas from any offered string in Python, the “substitute()” perform can be utilized. It could possibly return a reproduction string the place a substring’s presences is exchanged together with one other.

Syntax

The overall syntax of the “substitute()” methodology is:

str.substitute(oldValue, replaceValue)

 

Right here:

  • oldValue” is utilized to cross the present character to the substitute perform that requires it to be eliminated. We are going to remove the comma as the primary enter parameter.
  • replaceValue” argument is utilized to cross the character, changing the “oldValue” character.

To discover the performance of the above-discussed methodology, take a look at the next instance!

Instance

We’ve got used the beforehand created “original_string” variable string. Then, make the most of the “substitute()” methodology that takes the required argument and passes them to the “resultant_string” variable:

resultant_string = original_string.substitute(“,”, “”)

 

Get the modified string by using the “print()” assertion:

 

Output

That’s all! We’ve got compiled the completely different strategies to remove commas from any string in Python.

Conclusion

To take away the commas from any string in Python, the iterative perform “for” loop, the “re.sub()” methodology, and the “substitute()” methodology can be utilized. The “for” loop is the iterative perform that can be utilized for iterating over a sequence. The “re.sub()” perform is used for substitute, swapping, or eliminating the commas from the strings, and the “substitute()” perform returns a reproduction string the place a substring’s presences is exchanged together with one other. This put up demonstrated the methods of eradicating commas from the string in Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments