+ 1

Why is the code not working?

message = 'd89%l++5r19o7W *o=l645le9H' message = message.replace(' ', '') unwanted_characters = """-':";,?)(*&^%$#@!+×÷=/_€£¥₩1234567890`~\|<>}{][""" for c in message: if c in unwanted_characters: message = message.replace(unwanted_characters, '') print(message)

1st Sep 2024, 10:06 PM
Guilherme Cardoso
Guilherme Cardoso - avatar
4 Respuestas
+ 3
Guilherme Cardoso message = 'd89%l++5r19o7W *o=l645le9H' # do not remove the empty space # escape the ' \ '. change '\' to ' \\' unwanted_characters = """-':";,?)(*&^%$#@!+×÷=/_€£¥₩1234567890`~\\|<>}{][""" for c in message: if c in unwanted_characters: # use (c,''), not (message,'') message = message.replace(c, '') # reverse the message message = message[::-1] print(message)
2nd Sep 2024, 12:22 AM
Bob_Li
Bob_Li - avatar
+ 3
Another way to handle escape character. txt = r'\n' # raw string print(len(txt)) # 2 for t in txt: print(t) # \ # n
2nd Sep 2024, 2:59 AM
Wong Hei Ming
Wong Hei Ming - avatar
+ 3
Wong Hei Ming nice. I always forget about raw strings. but you still have to escape " or ', depending on which one you use as enclosure. unwanted_characters = r"-':\";,?)(*&^%$#@!+×÷=/_€£¥₩1234567890`~\|<>}{][" or unwanted_characters = r'-\':";,?)(*&^%$#@!+×÷=/_€£¥₩1234567890`~\|<>}{]['
2nd Sep 2024, 3:26 AM
Bob_Li
Bob_Li - avatar