You want to replace it, not strip it:
s = s.replace(',', '')
Answer from eumiro on Stack OverflowYou want to replace it, not strip it:
s = s.replace(',', '')
Use replace method of strings not strip:
s = s.replace(',','')
An example:
>>> s = 'Foo, bar'
>>> s.replace(',',' ')
'Foo bar'
>>> s.replace(',','')
'Foo bar'
>>> s.strip(',') # clears the ','s at the start and end of the string which there are none
'Foo, bar'
>>> s.strip(',') == s
True
regex in Python to remove commas and spaces - Stack Overflow
Efficient Way To Remove Repeating Commas From String
how to remove a space before a comma?
removing whitespace - How can I remove spaces before commas in python without regexes? - Stack Overflow
you can use the split to create an array and filter len < 1 array
import re
s='word1 , word2 , word3, '
r=re.split("[^a-zA-Z\d]+",s)
ans=','.join([ i for i in r if len(i) > 0 ])
How about adding the following sentence to the end your program:
re.sub(',+$','', test_string)
which can remove the comma at the end of string
I have a string that consists of something like:
my_string = 'Now is the time, , for all good men, , ,to come to the aid,, of their party'
...and I want to keep only a single comma for each repeating set of commas:
result = 'Now is the time, for all good men, to come to the aid, of their party'
I've looked a numerous methods to remove sequential characters, but 1) these are not sequential (may have one or more blanks between them); and 2) I only want to remove the extra commas and not other repeating characters that might appear in the string.
Any help would be greatly appreciated.
Hi :D
TOTAL noob here.
How to remove a space before a comma? For example here:
name=input("wuts ur name? ")
print("holy shit", name, ",", "your name has", name.lower().count("a"), "times the letter a!")So for example, if I input "Alexandra", it'll return:
holy shit Alexandra , your name has 3 times the letter a!
but as you can see, there is a space before the comma... how do I remove it? I tried several different things and searched on the internet too, but somehow couldn't find how welp XD
Thanks <3
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.
You can use string format :
print(f"{contact.lastname},{contact.firstname},{contact.email},{contact.phone}")
Or if you want to keep multiple parameters in the print funciton, use the sep (separator) parameter :
print(a, b, c, sep=',')
This can be done without regex:
>>> string = "Special $#! characters spaces 888323"
>>> ''.join(e for e in string if e.isalnum())
'Specialcharactersspaces888323'
You can use str.isalnum:
S.isalnum() -> bool Return True if all characters in S are alphanumeric and there is at least one character in S, False otherwise.
If you insist on using regex, other solutions will do fine. However note that if it can be done without using a regular expression, that's the best way to go about it.
Here is a regex to match a string of characters that are not a letters or numbers:
[^A-Za-z0-9]+
Here is the Python command to do a regex substitution:
re.sub('[^A-Za-z0-9]+', '', mystring)