When you're working with strings, a common operation that you might want to be able to perform is to take one string and split it into a list of sub strings. Similarly, you might already have a list of strings and you want to join those strings into one larger string. So, in this video, we're going to talk about how to split strings apart and join them back together. Let's get started. In order to split up a string, we first need to have a string. So, I have my sentence here. I encourage you to read it. And that's what we're going to split up. Now, how do we do this in Python? Well, conveniently, Python strings have a method called "split," which, as you might expect, splits up a string. Let's just run it and see what it does first, then we can talk about it. So it looks like it actually just broke my sentence up into words. That must be pretty smart. Well, yes and no. What it's actually doing is splitting based on white space. Okay. So what the split method does is it takes the input string and it breaks it up everywhere that it sees white space; a space, a tab, newline, things like that. And so it puts each element in the returned list, is a sequence of characters that don't include any white space. And then it sees white space, then it goes onto the next element of the list. So we do have, basically, words here, but it's not super intelligent in what it defines as a word, for instance. It's just breaking it up based on white space. Now, that's what happens when I call split with no arguments. But split actually can also take an argument. So, let's look at words, too, here. Now, I'm explicitly telling it to split whenever it sees a space. I would expect this to behave pretty much the same, and in fact, it does on this sentence. But now, it's not going to split on other kinds of white space. So you might try to change sentence around and see if you can make it have different splits from words and words, too. Perhaps more interestingly, instead of trying to recreate what white space means, I can pass different characters, like a comma. So if I want to break this sentence up into phrases, I want to split on commas. And if I look at what happens there, now you can see, right, she wasn't doing a thing that I could see, and there's a comma. It's now breaking it up, except standing there and leaning on the balcony railing. We scroll over here. We see that's the next element of the list. And then, finally, holding the universe together comes afterwards. And so, now you see that the spaces remain in the strings that are split. I also want to point something out here. The commas disappeared. The commas aren't in the output list. That may or may not have been completely obvious before, but whatever you're splitting on, that's just a marker to say, hey, this is the- the place to which you should stop and now start the next element of the list. It is not placed anywhere in the list. Now, I have a list of three phrases here. Maybe not exactly what you want, some of them begin with spaces, which is perhaps not if you were actually trying to break things up into phrases you would want. But split is not, you know, super intelligent. It just takes whatever character you give it and it uses that to split the string apart. Let's try something else. I want parts of the sentence, and I'm going to split on the string "the." Well, let's see what happens there. Now, whenever the word "the" occurs in the sentence, it removes that word and puts the string there. Wait a minute. It didn't look for the word "the. " It didn't have spaces around it. So "there" is a word. It saw that as "the." She wasn't doing a thing that I could see, except standing, and it gets rid of the of "the" from there and says "re" or releaning on. Then there's actually the word "the" splits on that, balcony railing holding where "the" again, universe togeer. The word "the" appears inside of together, and then we get an R at the end. Maybe this is what you want, maybe it's not. But in general, if I'm trying to split up strings, if I have characters that I want to use to break it apart, I can do so. It's not so intelligent that it understands the English language or anything, and is trying to do something that's grammatically correct, for instance, and how it breaks things up. It sees the characters T_H_E somewhere, whether they're inside of a word, not inside of a word, that's what it's going to split upon. This can still be valuable. Usually, you're going to use it with commas, or semi-colons, or spaces in order to break strings up like that. The split takes a longer string and allows me to split it up into a list of sub strings. I can also join strings back together. So let's say that I'm starting with a list of strings. My items here, I have a couple of random strings put together. I can join them together. Let's try that. Python has a method called "join." Hopefully, just like split, the name makes it pretty obvious what it's going to do. So it takes a list as an argument. I had to have this empty string in front of here. Well, join is a method on strings, not lists. So I have to have the string to join together. It doesn't quite make sense. Let me run this, and then we can talk about what's actually happening. I had my six strings. I called join on an empty string with my list. And what happened is, I have them all mashed together now. I've got flowers, puddle, mouse pad, outlet, bread, house and together. What the empty string actually was is it was telling Python what you want to put in between each item when you join them. So, maybe that's not exactly what I want. Let's try this. Let's have a string that is a space and let's join that way. We do that. Now, there are spaces between the strings that I joined together; flowers, space, puddle, and so on. Maybe that makes more sense. Or you could decide here, I would like to have a comma in between. Now, I've got flowers, puddle, mouse pad, outlet. Well, maybe, I actually wanted a comma and a space. I could do that, too. Now, I have a string that perhaps looks the way you might want a list of items to look. In different situations, any of these could have been what you were looking for. And you can put any string in between the items that you are joining together, and you just put that in the initial string then you call join on it. Python strings have these two powerful methods, split and join, that allow you to take a string and break it apart into a list of sub strings, or take a list of strings and combine them together into one longer string. Now, they don't always do exactly what you want, so they're probably, in most programs, building blocks for something more sophisticated. But they do break things up and join them back together in powerful ways. And so, if you use them correctly, hopefully, you can do the kinds of string processing that you'd like to do inside of your programs.