Its because you’re using a list comprehension, instead you just have to have that replace function, like this…
def printer_error(s):
    allowed = "abcdefghijklm"
    for char in s:
        if char not in allowed:
            s = s.replace(char, "")
    return s
print(printer_error("xyzabcdef"))
10
solved Why I can’t delete everything except for certain characters using a loop