0

I am attempting to strip an incoming message of all punctuation in order to run an accurate moderation system. However, when I use string.strip(), it only pulls off the ending character of the string, and leaves the rest of the same character in the remainder of the string. What am I misunderstanding? Every resource I can find says that string.strip() is sufficient.

Relevant code:

 async def on_message(message):

    stripString = ".!?,'@#$%^&*()"

    if message.author == bot.user:
        return
    
    message.content = message.content.strip(variables.stripString)

input: This. Is. A. Test.

output: This. Is. A. Test

azro
  • 53,056
  • 7
  • 34
  • 70
Shadrock50
  • 21
  • 3
  • `.strip()` only removes characters from the start or end of a string – jezza_99 Oct 28 '21 at 20:24
  • What is the expected output ? You want to remove all punctuations everywhere ? – azro Oct 28 '21 at 20:24
  • What is "every resource" to you? How about the python reference manual? https://docs.python.org/3.4/library/stdtypes.html?highlight=strip#str.strip It clearly states what strip does and what it doesn't do. – ypnos Oct 28 '21 at 20:25
  • Yeah, I want to remove all punctuation throughout the entire string. Apparently .strip() doesn't work, so what would I do instaead? – Shadrock50 Oct 28 '21 at 20:26
  • This SO thread might help. https://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python – troy Oct 28 '21 at 20:28

1 Answers1

4

The str.strip

Return a copy of the string with the leading and trailing characters removed.


What is you need is a replacement, using the regex [.!?,'@#$%^&*()] and replace by nothing

import re

stripString = ".!?,'@#$%^&*()"
message = "!This. !Is. A. Test.?@"

print(message.strip(stripString))                    # This. !Is. A. Test
print(re.sub("[" + stripString + "]", "", message))  # This Is A Test
azro
  • 53,056
  • 7
  • 34
  • 70