In Python, strings are immutable, so you have to create a new string. You have a few options of how to create the new string. If you want to remove the 'M' wherever it appears:
newstr = oldstr.replace("M", "")
If you want to remove the central character:
midlen = len(oldstr) // 2
newstr = oldstr[:midlen] + oldstr[midlen+1:]
You asked if strings end with a special character. No, you are thinking like a C programmer. In Python, strings are stored with their length, so any byte value, including \0, can appear in a string.
Using .replace is alright, but it looks ugly. I usually have to make a function for removing a character so that it doesn't look so ugly. Is there a built-in method to remove characters from a string?
In Python, strings are immutable, so you have to create a new string. You have a few options of how to create the new string. If you want to remove the 'M' wherever it appears:
newstr = oldstr.replace("M", "")
If you want to remove the central character:
midlen = len(oldstr) // 2
newstr = oldstr[:midlen] + oldstr[midlen+1:]
You asked if strings end with a special character. No, you are thinking like a C programmer. In Python, strings are stored with their length, so any byte value, including \0, can appear in a string.
To replace a specific position:
s = s[:pos] + s[(pos+1):]
To replace a specific character:
s = s.replace('M','')
Strings are immutable. str.replace creates a new string. This is stated in the documentation:
str.replace(old, new[, count])Return a copy of the string with all occurrences of substring old replaced by new. [...]
This means you have to re-allocate the set or re-populate it (re-allocating is easier with a set comprehension):
new_set = {x.replace('.good', '').replace('.bad', '') for x in set1}
P.S. if you're using Python 3.9 or newer, use str.removeprefix() or str.removesuffix() instead:
new_set = {x.removesuffix('.good').removesuffix('.bad') for x in set1}
>>> x = 'Pear.good'
>>> y = x.replace('.good','')
>>> y
'Pear'
>>> x
'Pear.good'
.replace doesn't change the string, it returns a copy of the string with the replacement. You can't change the string directly because strings are immutable.
You need to take the return values from x.replace and put them in a new set.
I have been trying to get this one working all day. I have a string that contains a substring that I would like to remove but the typical replace function isn't working in the way I would like to use it. I have the substring I want to remove stored in a string variable while having the string I am modifying in another variable. I tried to use the function like this
inspected = replace(substring, '')
but for some reason I get an error indicating that only one argument was passed rather than two. I think this function only accepts quotes rather than the variables themselves unless I am doing something wrong. I have been searching for this one all day but to no avail.
example
inspected = "iamthewalrus" substring = "walrus" inspected = replace(substring,"")