Having a variable with a large message, how to ignore exclamation marks and symbols while applying a string method to the words that are part of this message?
Example:
message = "THIS IS A MESSAGE!!, A TEXT *THAT HAS MANY-- WORDS- "
The above should become:
"this is a message a text that has many words"
I did it as shown below:
ignore_stuff = message.replace('*',"").replace('--',"").replace(",","").replace("!","")
turned_to_lower = ignore_stuff.lower()
print(turned_to_lower)
which gives the wanted result:
this is a message a text that has many words
Is there a better way that still does not use any external libraries and also does not use regular expressions?
>Solution :
You can use .isalnum() and .isspace()
message = "This is a sentence --- and this is great."
message = "".join(ch.lower() for ch in message if ch.isalnum() or ch.isspace())
message = " ".join(message.split())
print(message)