Python (Fall 2026) Module 9-5: Algorithms and Solving A8 Part 2
Overview
Bill Moseley refactors email-address cleanup in Python by creating a reusable `cleanHalf` function that keeps alphanumeric characters plus periods, hyphens, and underscores, stopping at the first invalid character. He applies it to both halves of an address, restores the reversed first half, joins the halves with `@`, and returns the cleaned string from `cleanEmail`.
Key takeaways
- A helper such as `cleanHalf` avoids duplicating character-filtering code when the same operation must be applied to both sides of an email address.
- The `cleanHalf` character policy accepts alphanumeric characters plus `.`, `-`, and `_`; an invalid character ends processing with `break`.
- Cleaning a reversed string does not restore its order, so the first half must be reversed again before the address is reassembled.
- Joining the cleaned halves with `@` directly is simpler here than rebuilding a list and using `join`.
- Returning `strClean` makes `cleanEmail` usable by other code, while printing is only useful for temporary inspection.
- The demonstrated filter can leave a trailing period in some results, so the cleanup algorithm still needs an additional rule for that case.
Chapters
- Bill Moseley extracts repeated validation work into a `cleanHalf` function called from `cleanEmail`.
- The function loops through a half-string and keeps alphanumeric characters and the permitted punctuation `.`, `-`, and `_`.
- When a character is invalid, `break` stops the loop so no later characters are added; the function returns the cleaned string.
- `cleanEmail` passes `strFirstHalf` and `strSecondHalf` to `cleanHalf`, replacing each variable with the returned cleaned text.
- Because the first half was reversed earlier in the algorithm, Moseley reverses it again after cleaning to restore its original order.
- The reusable function keeps the filtering logic in one place rather than duplicating the character-checking loop.
- The two cleaned halves are joined with `strFirstHalf + "@" + strSecondHalf` to form `strClean`.
- Moseley prints the assembled result for a check, then changes `cleanEmail` to return `strClean` instead of printing it.
- He notices some sample results end in a period and flags that trailing punctuation still needs to be handled.
Transcript excerpt
0:01 So that's what I want there. So the first half is reversed. Second half is not reversed. Now I'm going to do the same thing to this that I am to this. Anytime you say I'm going to do the same thing more than one time, your brain should immediately go to, I'm going to create a function so I don't have to type the same code twice. So, I'm going to create another function that's going to be called from cleanEmail. And I'm going to create it up here, and it's going to be called cleanHalf. The idea is that it's going to, this function is going to be in charge of doing this work. For each letter, check and see if it's valid, and if it is, add it to a return string. If it's not, break the loop and return. Okay. So what I'm going to do is I'm going to take that half,
0:56 str, as a string, and I'm going to say strClean, cleanHalf, ch. Remember, these variables live only inside the function. What happens in the function stays in the function. So it doesn't matter what I call them, really, because they only live inside that function. So I'm going to start out with empty string. Remember when I cleaned out that Bakersfield College string? I threw away all the punctuation, kept the words. We're going to do the same thing here. I'm going to say for strLetter in str. So for each letter in the half, we're going to say is, or if strLetter is numeric,
1:52 or strLetter in, if there's an str, if, if it's in this string. So, I have a hyphen, underscore, period, and that's really it, right? Because I'm looking back at this, this one. There won't be an at sign. If there's another at sign in my, in my,
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.