Python (Fall 2026) Module 2-7: Transforming Strings
Overview
Bill Moseley introduces Python string methods as operations that return transformed strings without changing the original. He demonstrates case conversion, whitespace removal with `strip()`, self-assignment and method chaining, then previews `replace()` and `find()` for editing and searching text.
Key takeaways
- Python string methods return transformed strings and leave the original string unchanged, so assigning the result is necessary to keep the transformation.
- `strip()` removes leading and trailing whitespace, including spaces, tabs, and line breaks, but does not remove spaces within a string.
- Self-assignment such as `text = text.strip().lower()` reuses one variable while applying multiple transformations.
- Method chaining can clean input as it is captured, for example by calling `.strip().lower()` on the string returned by `input()`.
- `replace()` substitutes character sequences, and `find()` searches for a substring, making them useful for editing and checking text.
Chapters
- String methods such as `lower()`, `upper()`, and `swapcase()` operate on a string and return a transformed version rather than modifying the original.
- `strip()` removes whitespace from the beginning and end of a string, including spaces, tabs, and line breaks.
- Bill Moseley emphasizes that string is the object on which these methods must be called.
- Copying a password can accidentally include a leading or trailing space, causing login failures; `strip()` can remove that unintended whitespace.
- A prompt such as `What is your name?` can encourage users to type a leading space, so input should be cleaned before further processing.
- Whitespace inside a string is preserved by `strip()`, so a name or phrase with internal spaces remains intact.
- Self-assignment, such as `strPass = strPass.strip()`, replaces the variable's value with its cleaned version without creating extra intermediate variables.
- Method chaining applies multiple transformations in one expression, for example `input(...).strip().lower()` before storing the result.
- `replace()` returns a string with selected character sequences substituted, while `find()` searches a string for a substring such as an email address in an essay.
- The closing examples reinforce that `strip()` affects only the ends of text, while `lower()` and `upper()` change letter case.
Transcript excerpt
0:00 So we have lots of string methods, lots and lots of them. These are some of the ones I want to introduce you to. Now, am I going to tell you every single string method today? Yes, it'll be on the quiz next week. Just kidding. No quizzes. Quizzes are stupid. Especially for programming. Not just for programming, but especially for programming. Will I tell you? No, I'm not going to tell you all the string methods. You can look them up yourself, but I'm going to tell you some of them that will give you a good sense of how they're used and how you can use them. So we already talked about the second one on this list, string. And string in this instance is just a word that I'm using to refer to the variable, to remind us that all of these methods have to be using a string as what we would call the context of that.
1:00 So in other words, they have to be something that. It's an ability that a string has. So every time we use a method that's a string method, we have to use that on a string. Other thing to remember is that the string that you use it on doesn't get changed. It just returns a version of that string that has this done to it. So we talked about the second one. The second one, string.lower(), returns a lowercase version of that string. What do you think string.upper() does? Uppercase. Yeah. If you said it makes it really hyper and, no, that would be the wrong kind of upper. Uppercase. string.swapcase(). So it makes everything that's uppercase lowercase
1:57 and everything that's lowercase uppercase. Why would you use that? Oh, there's a lot of different reasons, but a lot of them have to do with analyzing a string. So that's a useful one.
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.