Python (Fall 2026) Module 8-4: String Slicing
Overview
Bill Moseley explains how Python strings can be processed as iterables and sliced using index-based notation. He demonstrates cleaning noisy text by retaining letters and spaces in a reusable function, then covers zero-based indexing, stop-exclusive slices, negative indices, omitted bounds, step sizes, and reversing a string with `[::-1]`.
Key takeaways
- Python strings are iterable, making a character-by-character `for` loop a straightforward way to filter noisy text.
- A cleanup function that accepts text and returns a cleaned string can be reused across programs instead of rewriting the filtering logic.
- Python slice stops are exclusive: `text[2:5]` returns the characters at indexes 2, 3, and 4.
- Negative indexes and omitted slice bounds let code select text relative to the string’s end, such as `text[start:]` or `text[:-2]`.
- Slice steps control which characters are selected: `text[::2]` takes every second character, while `text[::-1]` reverses the string.
Chapters
- Python strings are iterables, so a `for` loop can examine each character in a string such as noisy text containing “Bakersfield College.”
- Retain alphabetic characters and spaces while discarding punctuation and other unwanted characters.
- Use `print(end=...)` to change what `print()` adds after each character, or build a cleaned result with `strClean += strLetter`.
- Wrap the cleanup logic in a `cleanText(strText)` function that returns the cleaned string for reuse.
- Strings support zero-based indexing: `strText[6]` retrieves the seventh character, `f`, in “Bakersfield College.”
- A slice such as `strText[2:5]` includes index 2 and stops before index 5, returning three characters.
- Python strings support indexing and slicing but cannot be modified in place.
- Negative indexes count from the end, so a slice ending at `-2` excludes the final two characters.
- Omitting the stop index, as in `strText[intIndex:]`, returns the text from a chosen position through the end.
- The slice `strText[::2]` traverses the entire string in steps of two, selecting indexes 0, 2, 4, and so on.
- The reverse slice `strText[::-1]` returns the entire string in reverse order, a useful technique for string manipulation.
Transcript excerpt
0:00 I'm going to give you a few more tools, maybe more than five crayons. Maybe I'll give you the box of 16. So, strings are iterables. What's, what does that mean? Strings are basically just a list of characters that you can go through one at a time. But that gives us a lot of power, right? Look at this code. So, we have this text. Bakersfield College is in there somewhere. You can see that. But there's also a lot of garbage. This is really common. Okay. When you're dealing with text and data from the real, real world, expecting it to be clean and usable is ridiculous and will lead to failure 100% of the time. If you're working with real data, right, you're going to get all kinds of garbage in there because users enter garbage. If we
0:50 have sensors that are collecting data from the environment, they're getting false positives. If I set up a microphone that was supposed to capture bird sounds on campus and turn that into data, what do you suspect it would also capture, right? People yelling, like the bells that toll every hour, cars driving by, airplanes, you're getting all kinds of garbage data, but that's, that's how it always is. So when you have text data, this is a really common scenario. What do you want to keep? You want to keep Bakersfield College. How do you know the difference? Well, we know that Bakersfield College doesn't contain punctuation. So, anything that's not a letter, I can get rid of it. So, what do I do? I'm going to go through this one character at a time. And I'm just going to say, is that a letter? Is that a letter? Is that a letter? And if the answer is yes,
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.