Home > Blockchain >  Ignore non-words and apply a string method to words
Ignore non-words and apply a string method to words

Time:01-06

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?

CodePudding user response:

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)

CodePudding user response:

Similar to S P Sharan's answer, but doing it with only one join:

message = "THIS IS A MESSAGE!!, A TEXT *THAT HAS MANY   -- WORDS- "
clean = "".join(
    last:=l.lower() for l in message.strip()
    if l.isalnum() or (l.isspace() and not last.isspace())
)
print(clean)

CodePudding user response:

What you can do is make a list of letters and remove anything in the string that is not in the list. Like this:

letters = [' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
message = "THIS IS A MESSAGE!!, A TEXT *THAT HAS MANY-- WORDS- "
changed_message = ""
for l in message:
    if l.lower() in letters:
        changed_message  = l.lower()
  •  Tags:  
  • Related