1
00:00:00,000 --> 00:00:04,990
Splitting and joining strings is a very common operation, especially in text

2
00:00:05,000 --> 00:00:08,990
based environments like the World Wide Web or when you are operating on a lot of text files.

3
00:00:09,000 --> 00:00:13,990
So Python provides some string methods for splitting and joining strings.

4
00:00:14,000 --> 00:00:14,990
Let's take a look at how that works.

5
00:00:15,000 --> 00:00:17,990
We'll take a string. We'll put it in a variable because we are going to be using

6
00:00:18,000 --> 00:00:19,990
it here quite a bit.

7
00:00:20,000 --> 00:00:25,990
And we'll say s = 'This is a string of words.'

8
00:00:26,000 --> 00:00:30,990
And if we want to split that string apart we can just say s.split.

9
00:00:31,000 --> 00:00:33,990
And it'll split the string apart on the white space.

10
00:00:34,000 --> 00:00:37,990
And in fact if there was a lot of white space in between the words, if 'tThis

11
00:00:38,000 --> 00:00:46,990
string was something like this is a string of words' like that .split.

12
00:00:47,000 --> 00:00:53,990
We see that it folds all the white space together first before splitting on the white space.

13
00:00:54,000 --> 00:00:56,990
So that's a very nice and handy thing to do.

14
00:00:57,000 --> 00:01:01,990
If we wanted to split on some other token besides the white space we can specify

15
00:01:02,000 --> 00:01:05,990
it in the split method but it won't do this folding thing.

16
00:01:06,000 --> 00:01:08,990
That folding thing is a special thing that it does just with the white space.

17
00:01:09,000 --> 00:01:11,990
And it only does that if you don't specify an argument to split it all.

18
00:01:12,000 --> 00:01:20,990
For example if I say s.split and I want to split it on say the i characters like that,

19
00:01:21,000 --> 00:01:24,990
then I'll get back a list of the string with all of the i's removed and the

20
00:01:25,000 --> 00:01:28,990
words separated on the i characters and you see the space is still in there.

21
00:01:29,000 --> 00:01:33,990
So let's go ahead and split it on the white space and assign it to a variable.

22
00:01:34,000 --> 00:01:38,990
We'll call the variable words = s.split like that.

23
00:01:39,000 --> 00:01:42,990
And now we have a list words is a list with a bunch of separate words in it.

24
00:01:43,000 --> 00:01:46,990
And it's utterable. I can say for w in words:

25
00:01:47,000 --> 00:01:50,990
print(w) and it'll print those words out separately.

26
00:01:51,000 --> 00:01:53,990
But I can also join them back together if I want to.

27
00:01:54,000 --> 00:01:59,990
I can say new string =, doing it with a colon in it, .join and so the join

28
00:02:00,000 --> 00:02:03,990
method operates on the token that's going to be used to join things back

29
00:02:04,000 --> 00:02:05,990
together not on the list of words.

30
00:02:06,000 --> 00:02:09,990
And so I say join and I say words like this.

31
00:02:10,000 --> 00:02:12,990
And now I have a new string, which is all of those words joined back

32
00:02:13,000 --> 00:02:13,990
together with colons.

33
00:02:14,000 --> 00:02:19,990
Or if I wanted to I could say put a string with a comma and a space in it

34
00:02:20,000 --> 00:02:21,990
and say .join(words).

35
00:02:22,000 --> 00:02:25,990
And I would get back a list of words with a comma and a space.

36
00:02:26,000 --> 00:02:28,990
So I can really put whatever I want to there in the string that I am going to

37
00:02:29,000 --> 00:02:29,990
use to join the words back together.

38
00:02:30,000 --> 00:02:34,990
So in a nutshell that's how we split and join strings in Python.

39
00:02:35,000 --> 00:02:36,990
Again this is a very common operation.

40
00:02:37,000 --> 00:02:40,990
This is something that you'll use quite a bit if you are working with any text files.

41
00:02:41,000 --> 00:02:42,990
If you are working on a World Wide Web, if you are working with mail, if you are

42
00:02:43,000 --> 00:02:44,990
doing any manipulation of text whatsoever.

43
00:02:45,000 --> 00:02:55,000
You are going to find a lot of uses for split and join.

