Python (Fall 2026) Module 8-2: String Methods for Manipulation
Overview
Bill Moseley explains how Python's `str.split()` breaks text into a list, including how its separator and `maxsplit` arguments control the result. He builds a reusable `wordCount` function that returns 13 for an example sentence, then demonstrates `str.join()` to combine list elements using a chosen separator.
Key takeaways
- Python's `str.split()` with no arguments divides text on whitespace and returns a list suitable for further processing.
- A basic word counter can return `len(sentence.split())`; Moseley's example reports 13 words.
- The separator passed to `split()` is removed from the resulting pieces, while `maxsplit` limits how many divisions occur.
- When a requested separator is absent, `split()` returns the original text as a single list element.
- Python's `separator.join(iterable)` combines string elements with the chosen separator, such as joining words with `@` instead of spaces.
Chapters
- Calling `split()` without arguments separates a string on whitespace and produces a list of words.
- Taking the list's length gives a basic word count; the example sentence contains 13 words.
- Bill Moseley turns the approach into a reusable `wordCount(strSentence)` function that returns the count.
- Passing a separator such as `"."` or `"E"` makes `split()` divide the string at that character.
- The separator is removed from the resulting pieces, so splitting on spaces leaves no spaces in the list.
- Using `maxsplit=2` limits the operation to two splits and leaves the remainder together in the final piece.
- If the separator does not occur in the string, `split()` leaves it unsplit.
- The separator string calls `.join(iterable)` to combine list elements into one string.
- Moseley splits a sentence on spaces, then uses `"@".join(liText)` to place an at sign between the words.
- Choosing a different join separator can transform text, including replacing repeated separators after splitting.
Transcript excerpt
0:01 So string methods are, string methods that we have available to us for manipulating strings can help us do things with a string. For example, string.split allows us to take a string and break it up into a list using any character we want as a separator. Now this uses a default argument of the separator, and there's also another default in maxsplit. So if I don't pass it any parameters, what it does is it splits a string on the space, and it will split as many times as there are spaces in the string. But I could also tell it to split on each period in, in the sentence. And I could tell it only
0:57 split one time if I wanted to. string.split is very useful, because if I have a string like this one here, I do this, I turn it into a list using string.split. So strText.split. I can print out that list. Oops, wrong program. Let's use the current file. How about, there it is. So there's my string split into a list. Now you can see it's, it looks just like a list,
1:52 a list of strings, but that's pretty useful for a lot of things. Now I have a list of all of the words in this string. Guess what I can do? I can print. If I print the length of that, I can tell you how many words are in that string. So I can feed it now any type of string that I want, and it will tell me how many words. So right here I could very easily create a function from this that will tell me how
2:50 many words are in any string that I feed it. So let's try that. So if I say def wordCount, strText,
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.