I try to find all characters from the given list and replace them with one character. I`m getting "object has no attribute 'replaceall' ?
list = 'bcdfghjklmnpqrstuvwxyzBCDFGHJKLMNPQRSTUVWXYZ'
string = "aabbccddeeff"
string2 = string.replaceall(list, "!")
print(string2)
I would like to get: !!bbccdd!!ff as a and e are not on the list.
There is no "str.replaceall()". There is a "str.replace()" method, but it doesn't do what you want to do.
Output:
>>> help(str.replace)
Help on method_descriptor:
replace(self, old, new, count=-1, /)
Return a copy with all occurrences of substring old replaced by new.
count
Maximum number of occurrences to replace.
-1 (the default value) means replace all occurrences.
If the optional argument count is given, only the first count occurrences are
replaced.
There is the re (regular expressions) library. It has a function named "sub()" that looks like it will do what you want.
https://docs.python.org/3/library/re.html
Search for "complementing" in the section about Regular Expression Syntax..
You can do
from string import ascii_letters
spam = 'bcdfghjklmnpqrstuvwxyzBCDFGHJKLMNPQRSTUVWXYZ'
trans = str.maketrans({char:'!' for char in ascii_letters if char not in spam})
eggs = "aabbccddeeff"
foo = eggs.translate(trans)
print(foo)
Docs on
str.maketrans() and
str.translate()