+ 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)
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)
+ 3
Another way to handle escape character.
txt = r'\n' # raw string
print(len(txt)) # 2
for t in txt:
print(t)
# \
# n
+ 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`~\|<>}{]['