Answer to Question #157391 in Python for Andu

Question #157391

3. Write a python function called removePunctuation that takes as an argument a string. representing a sentence and returns a copy of the string with all punctuation removed.

Sample:

Argument: "Hello?! Let's try to solve this exercise, it will be easy :)!"

The function will return: Hello Lets try to solve this exercise it will be easy (Hint: You may use string built-in functions: isalnum) to check for every character if the character is alphanumeric (letter or digit) and isspace) to check if it is a space. Remove any character that is neither an alphanumeric, nor a space)


1
Expert's answer
2021-01-23T17:12:04-0500
def removePunctuation(phrase):
    for chr in phrase:
        if not str.isalnum(chr):
            if not str.isspace(chr):
                phrase = phrase.replace(chr, "")

    return phrase

# Testing it out
if __name__ == '__main__':
    print(removePunctuation("Hello?! Let's try to solve this exercise, it will be easy :)!") == "Hello Lets try to solve this exercise it will be easy ")

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS