Complete the code to remove punctuation from the text using str.translate.
import string text = "Hello, world!" clean_text = text.[1](str.maketrans('', '', string.punctuation)) print(clean_text)
The translate method with str.maketrans removes all punctuation characters from the string.
Complete the code to remove all characters that are not letters or spaces using a list comprehension.
text = "Hello, world! 123" clean_text = ''.join([c for c in text if [1] or c == ' ']) print(clean_text)
The isalpha() method checks if a character is a letter. We keep letters and spaces only.
Fix the error in the code to remove punctuation using regex.
import re text = "Hello, world!" clean_text = re.sub([1], '', text) print(clean_text)
The regex '[^a-zA-Z ]' matches any character that is NOT a letter or space, so it removes punctuation and digits.
Fill both blanks to create a function that removes punctuation and converts text to lowercase.
import string def clean_text(text): return text.[1](str.maketrans('', '', string.[2])).lower()
The translate method removes all punctuation characters defined in string.punctuation. Then lower() converts text to lowercase.
Fill all three blanks to create a dictionary comprehension that maps words to their cleaned versions without punctuation and in lowercase.
import string words = ['Hello!', 'World?', 'Test.'] clean_words = {word[1]: word.[2](str.maketrans('', '', string.[3])).lower() for word in words} print(clean_words)
The dictionary comprehension uses translate with string.punctuation to remove punctuation, then lower() to convert to lowercase.