How do you remove spaces and commas in python?

string.replace[" ,",","]

You can use the replace method to substitute " ," with "," and then you have what you want.

Although, with your example, just do:

print[contact.lastname+",",contact.firstname+",",contact.email+",",contact.phone]

When you do print[v,w] you automatically add a space. So if you do print[v,",",w] you have the space you try to get rid of. If, instead, you do print[v+","] then you have no space between the text in v and the comma.

There are various ways to remove spaces from a string in Python. This tutorial is aimed to provide a short example of various functions we can use to remove whitespaces from a string.

Python Remove Spaces from String

Python String is immutable, so we can’t change its value. Any function that manipulates string value returns a new string, and we have to explicitly assign it to the string. Otherwise, the string value won’t change. Let’s say we have an example string defined as:

s = '  Hello  World   From Pankaj \t\n\r\tHi There  '

This string has different types of whitespaces as well as newline characters. Let’s have a look at different functions to remove spaces.

strip[]

Python String strip[] function will remove leading and trailing whitespaces.

>>> s.strip[]
'Hello  World   From Pankaj \t\n\r\tHi There'

If you want to remove only leading or trailing spaces, use lstrip[] or rstrip[] function instead.

replace[]

We can use replace[] to remove all the whitespaces from the string. This function will remove whitespaces between words too.

>>> s.replace[" ", ""]
'HelloWorldFromPankaj\t\n\r\tHiThere'

join[] with split[]

If you want to get rid of all the duplicate whitespaces and newline characters, then you can use join[] function with string split[] function.

>>> " ".join[s.split[]]
'Hello World From Pankaj Hi There'

translate[]

If you want to get rid of all the whitespaces as well as newline characters, you can use string translate[] function.

>>> import string
>>> s.translate[{ord[c]: None for c in string.whitespace}]
'HelloWorldFromPankajHiThere'

Python Remove Whitespaces from String using Regex

We can also use a regular expression to match whitespace and remove them using re.sub[] function.

import re

s = '  Hello  World   From Pankaj \t\n\r\tHi There  '

print['Remove all spaces using RegEx:\n', re.sub[r"\s+", "", s], sep='']  # \s matches all white spaces
print['Remove leading spaces using RegEx:\n', re.sub[r"^\s+", "", s], sep='']  # ^ matches start
print['Remove trailing spaces using RegEx:\n', re.sub[r"\s+$", "", s], sep='']  # $ matches end
print['Remove leading and trailing spaces using RegEx:\n', re.sub[r"^\s+|\s+$", "", s], sep='']  # | for OR condition

Output:

Remove all spaces using RegEx:
HelloWorldFromPankajHiThere
Remove leading spaces using RegEx:
Hello  World   From Pankaj 	
	Hi There  
Remove trailing spaces using RegEx:
  Hello  World   From Pankaj 	
	Hi There
Remove leading and trailing spaces using RegEx:
Hello  World   From Pankaj 	
	Hi There

You can check out the complete Python script and more Python examples from our GitHub Repository.

Reference: StackOverflow Question

  1. HowTo
  2. Python How-To's
  3. Remove Commas From String in Python

Created: August-01, 2021

  1. Remove Commas From String Using the replace[] Method in Python
  2. Remove Commas From String Using the re Package in Python

This tutorial explains how we can remove commas from a string using Python. To remove commas from a string in Python, we can use the replace[] method or the re package.

We will use the string in the code snippet below to demonstrate how we can remove commas from a string in Python.

my_string="Delft, Stack, Netherlands"
print[my_string]

Output:

Delft, Stack, Netherlands

Remove Commas From String Using the replace[] Method in Python

The replace[] method in Python str class replaces a substring with the specified substring and returns the transformed string.

Syntax of replace[] Method:

str.replace[old, new , count]

Parameters

old substring, which is to be replaced in the string str
new substring used to replace old substring in the string str
count optional parameter that specifies how many times old is replaced by new. If count is not provided, the method will replace all old substrings with new substring.

Return

String in which old substring is replaced by new substring.

Example: Remove Commas From String Using the str.replace[] Method

my_string="Delft, Stack, Netherlands"
print["Original String is:"]
print[my_string]

transformed_string=my_string.replace[",",""]
print["Transformed String is:"]
print[transformed_string]

Output:

Original String is:
Delft, Stack, Netherlands
Transformed String is:
Delft Stack Netherlands

It replaces all the commas in the string my_string with "". Hence, all the , in the string my_string are removed.

If we only wish to remove the first , in the my_string, we can do so by passing the count parameter in the replace[] method.

my_string="Delft, Stack, Netherlands"
print["Original String is:"]
print[my_string]

transformed_string=my_string.replace[",","",1]
print["Transformed String is:"]
print[transformed_string]

Output:

Original String is:
Delft, Stack, Netherlands
Transformed String is:
Delft Stack, Netherlands

As the value of count is set to 1 in the replace[] method, it only removes the first comma in the string my_string.

Remove Commas From String Using the re Package in Python

In the re pacakge of Python, we have sub[] method, which can also be used to remove the commas from a string.

import re

my_string="Delft, Stack, Netherlands"
print["Original String is:"]
print[my_string]

transformed_string=re.sub[",","",my_string]
print["Transformed String is:"]
print[transformed_string]

Output:

Original String is:
Delft, Stack, Netherlands
Transformed String is:
Delft Stack Netherlands

It replaces all the , in the string my_string with "" and removes all the commas in the string my_string.

The first argument to the re.sub[] method is substring to be replaced, the second argument is the substring to replace with, and the third argument is the string in which replacement is to be done.

Related Article - Python String

  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • How do you get rid of spaces in Python?

    The strip[] method is the most commonly accepted method to remove whitespaces in Python. It is a Python built-in function that trims a string by removing all leading and trailing whitespaces.

    How do you remove commas from sentences in Python?

    Use str. replace[] to remove a comma from a string in Python Call str. replace[',', ''] to replace every instance of a ',' in str with '' .

    How do I remove a comma from a text file in Python?

    write[infile. read[]. replace[',','']] will eliminate the commas.

    How do you remove spaces and special characters from a string in Python?

    Using 'str. replace[] , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace[] method will replace all occurrences of the specific character mentioned.

    Chủ Đề