Splitting and joining are two other useful methods on strings in lists.
So, split takes a string and turns it into a list of substrings of that string.
So, let's suppose that we have a string, leaders and best.
So, here we have a string leader,
and let's suppose that we call dot split on that string.
What dot split does,
if we call it with nothing as an argument,
it first looks for all the spaces in the string.
So, here it finds a space,
and here it finds a space,
and it cuts the string along the spaces.
So, what it does, is it turns this what was one string into three different strings,
and it divides them along the spaces.
So, the end result of this expression is going to be a list,
where the first item is leaders,
the second item is and,
and the third item is best.
One thing to note here is that split actually
does get rid of the spaces in between all of these.
So, this is leaders with no space afterwards and
no space before or after and or before and after best.
So, split essentially takes a string and splits it up into different words,
If it's called with no arguments.
So, let's try this out in code.
So, suppose that we have this string,
song equals "The Rain in Spain" and we say words equals song dot split.
Then when we call song dot split,
the value of this expression is going to split song along every space.
So, we're going to get a new list that has four items, The,
and then Rain in Spain dot dot dot.
So, when we print out words then we should get a four item list.
Now, we can also call dot split with an argument to specify what we want to split along.
So, let's suppose that we actually called dot split with
the argument e. Whatever argument we pass in here in this case e,
specifies what we actually want to split along.
So, here if we say we want to split along every e,
if we split the string, leaders and best,
then that's like crossing through every e that's in the string,
and then the result is that we get a list where the first item is l,
the second item is ad,
the third item is rs and, and so on.
So, let's try that out in code.
So here, we have the same song.
So, "The Rain in Spain" and let's suppose that we say song.split ("ai").
So, when we do that,
then we're going to search for every ai.
So, we find this ai, then this ai.
What we should get back is a list with three items, the,
space, r, n, space,
in, space, and so on.
You see that that's exactly what we get.
Now, the opposite of split,
which splits a string along lines is join.
Join takes a list of strings and joins it into one long string.
So, split is like chopping with a knife,
and join is like joining it back together with glue.
So, if we say I want to use this as the glue to join the items in this list,
what we get as the value of this overall expression is every item in this list,
leaders and best kind of glued back together with this string.
So, we get leaders slash and slash best.
So, let's try that out in code.
So here, we have a list of words,
red, blue, and green.
We specify a variable glue.
This variable glue is what's going to come in-between these items when we call dot join.
So, if we say glue.join(wds),
and assign that to s. Then when we print out s,
then we should expect to see red semicolon blue Semicolon green.
So, let's test that out.
I'm just going to comment out the rest of this code.
So, you can see that what we got was
the semicolon gluing back together the items and words.
But one thing to note is that calling
dot join doesn't effect the value of the list itself.
So, if we print out words after having called glue.join(wds),
then we still get out a list that has three items,
red, blue, and green.
Of course, we can also use a multi character string to actually join the words together.
So, if I use three stars to join words,
then I get red star star star,
blue star star star green.
I can even use an empty string to glue back the words together.
So, if I use an empty string dot join words,
then I get red, blue,
green all concatenated together.
So, let's go through some problems.
So, this question asks us to create a variable output that's assigned to a list,
whose elements are the words in string one.
So, in other words, we want to split up string one into individual words.
So, I want to assign output equal to str1.split.
If I call that split with no arguments,
then it splits it along the spaces.
This question asks us to create a variable called words and assign it
to a list whose elements are the words in the string sent,
and it tells us not to worry about punctuation.
So, I'm going to do something very similar here.
I'm going to say words equals sent.split.
Again, no arguments in dot split because we want to split it along the spaces.
Now words is set to a list of words in a sentence.
So, split and join are functions that
we'll keep coming back to you throughout this course,
and they'll end up being enormously useful.
That's all for now, until next time.