Free course · Lesson 3 of 5
Cleaning Text and Masking Personal Data
In this lesson you will
- Clean text with simple rules
- Mask phone numbers before analysis
Real messages are messy: capitals, repeated punctuation, abbreviations, phone numbers and emojis. Cleaning makes the same word look the same every time, and masking replaces personal details with placeholders before the text is analysed or shared.
- Lowercasing makes "UNPAID" and "unpaid" the same word.
- Regular expressions find patterns such as phone numbers and replace them with a placeholder like <PHONE>.
- Cleaning depends on the task: "!!!" carries feeling for sentiment analysis but is noise for topic classification.
Practical Skill: Keep the original text, write every cleaning step into one function, and test it on messages that should and should not change.
Python
import re text = "Hello, my receipt still shows UNPAID!!! Tel 0912 345 678"text = text.lower()text = re.sub(r"\b0\d{3}[\s-]?\d{3}[\s-]?\d{3}\b", "<PHONE>", text)text = re.sub(r"!+", "!", text)print(text) # Output:# hello, my receipt still shows unpaid! tel <PHONE>
Practice exercise
Rewrite three messy messages by hand: lowercase them, mask any phone numbers and remove repeated punctuation.
Key takeaways
- Mask personal data before anything else
- Cleaning choices depend on the task
Free self-study lesson — no certificate. For the full instructor-led program, see registration options.