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)
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 ")
Comments
Leave a comment