1
00:00:05,200 --> 00:00:10,459
The strip, lstrip, and rstrip methods are 
commonly used when processing text files. 

2
00:00:10,560 --> 00:00:15,404
We've already seen how to strip whitespace, 
including the newline, from the end of a line. 

3
00:00:15,520 --> 00:00:20,246
I'm including this video, because the exact 
behaviour of these methods isn't obvious. 

4
00:00:20,320 --> 00:00:25,273
Many students get confused, when they try 
to strip something and get strange results. 

5
00:00:25,360 --> 00:00:28,880
Create a new Python file called 
stripping.py for this video. 

6
00:00:28,880 --> 00:00:32,179
I'll start by reading the first 
line of the Jabberwocky file: 

7
00:01:01,920 --> 00:01:05,528
Run the program, to check the 
string that we'll be working with. 

8
00:01:05,760 --> 00:01:10,560
Note that you don't have to read every line from 
a file. We saw that in the last video, when we 

9
00:01:10,560 --> 00:01:15,680
broke out of the loop, and we're only reading one 
line here. After the first line has been read, 

10
00:01:15,680 --> 00:01:21,738
the with block exits and the file gets closed.
Ok, let's strip the apostrophe from the string. 

11
00:01:21,840 --> 00:01:26,688
I'll use strip for these examples, but 
the behaviour is the same for lstrip and rstrip. 

12
00:01:26,688 --> 00:01:30,312
The only difference is that strip 
will strip from both ends of the string. 

13
00:01:49,840 --> 00:01:54,794
Run the program, to confirm that no_apostrophe 
doesn't start with an apostrophe. 

14
00:01:55,200 --> 00:01:57,920
That's probably the behaviour 
you expected, but it can lead 

15
00:01:57,920 --> 00:02:01,979
you to believe that the string we stored in 
chars is removed from either end of first. 

16
00:02:02,160 --> 00:02:05,320
That's not what's happening here.
To demonstrate that, 

17
00:02:05,320 --> 00:02:10,899
I'll attempt to strip the apostrophe and the 
word "Twas" from the string. Change line 7: 

18
00:02:17,120 --> 00:02:21,840
That looks like it should 
work. Let's run it and see: 

19
00:02:22,880 --> 00:02:28,138
At first glance, it seems to have worked.
But what's happened to the end of the string? 

20
00:02:28,320 --> 00:02:34,045
The "s" of "toves" has been removed, as well!
This is the behaviour that can be confusing. 

21
00:02:34,240 --> 00:02:37,644
These strip methods don't remove the 
string that you pass as an argument. 

22
00:02:37,840 --> 00:02:41,440
They take each of the characters in 
the string, and keep removing them, 

23
00:02:41,440 --> 00:02:45,625
until a non-matching character is found.
The only character that matched, 

24
00:02:45,625 --> 00:02:51,200
at the end of the string, was the s. When strip got to 
the e in "tove", that doesn't match any of the 

25
00:02:51,200 --> 00:02:56,560
characters in our chars variable. So strip stopped 
removing characters from the end of the string. 

26
00:02:56,560 --> 00:03:01,191
I'll add some more characters to chars, so 
we can get a clearer idea of the behaviour: 

27
00:03:07,280 --> 00:03:11,040
Our chars string now contains 
the characters "e", "b" and "v", 

28
00:03:11,040 --> 00:03:14,960
in addition to the ones we had before.
That means any of the 8 characters 

29
00:03:14,960 --> 00:03:19,419
in chars will be removed, until strip 
reaches a character that isn't in chars. 

30
00:03:19,600 --> 00:03:25,072
The "b" of "brillig", and the "v" and "e" from 
the end of "tove", should also be removed. 

31
00:03:25,200 --> 00:03:29,712
Run the program, to check if that's what happens.
And it's not! 

32
00:03:29,840 --> 00:03:34,560
The "e" and the "v" were removed from the 
end, but we've still got the "b" of "brillig". 

33
00:03:34,560 --> 00:03:40,739
Pause the video, and see if you can work out why. 
Start the video again, to see if you were right. 

34
00:03:42,320 --> 00:03:46,915
Ok, did you work it out?
strip checks each character at the ends of the string,

35
00:03:46,915 --> 00:03:50,880
in turn. If a character 
is in the characters we told it to remove, 

36
00:03:50,880 --> 00:03:54,880
it removes it and moves on to the next.
It stops when it finds a character 

37
00:03:54,880 --> 00:03:59,440
that's not one that we told it to remove.
In this case, when examining the left hand 

38
00:03:59,440 --> 00:04:04,632
side of the string, it removed the apostrophe 
and "Twas", then it examined the space. 

39
00:04:04,800 --> 00:04:08,080
We didn't include a space in 
chars, so strip stopped processing 

40
00:04:08,080 --> 00:04:12,080
the left hand side of the string.
When processing the right hand side, 

41
00:04:12,080 --> 00:04:17,539
it removed the "s", the "e" and the 
"v". It then got to the "o" and stopped. 

42
00:04:17,600 --> 00:04:22,240
We don't have "o" in our chars string.
Add a space to chars, to confirm that 

43
00:04:22,240 --> 00:04:27,000
strip will remove any character it finds, 
until it finds something we didn't include: 

44
00:04:33,280 --> 00:04:39,165
I've included a space, between the apostrophe 
and the letter "T". Run the program again 

45
00:04:40,880 --> 00:04:45,593
and the space, and the letter "b", are 
now removed from the start of the string. 

46
00:04:45,840 --> 00:04:49,885
This is one of those things that, 
once you understand it, it's obvious. 

47
00:04:50,000 --> 00:04:53,378
But until you do understand 
it, it can be really confusing. 

48
00:04:53,440 --> 00:04:58,160
I'm sure some students are still a bit 
confused, so I'll include another explanation. 

49
00:04:58,160 --> 00:05:01,600
We'll write some code, to 
do the same job as strip. 

50
00:05:01,600 --> 00:05:05,782
Comment out lines 8 and 9, and 
we'll use a couple of for loops. 

51
00:05:06,960 --> 00:05:11,911
We'll use one for loop to process the start of 
the string, and another one to process the end. 

52
00:05:35,760 --> 00:05:41,232
Ok, that will tell us which characters will be 
removed from the start of the string. Run it 

53
00:05:42,720 --> 00:05:48,432
and we can see the behaviour of strip. It removes 
each of the characters, if they're in chars. 

54
00:05:48,560 --> 00:05:52,337
As soon as it finds a character 
that isn't in chars, it stops. 

55
00:05:52,640 --> 00:05:57,897
Processing the end of the string is very similar. 
I'll print some asterisks to separate the output: 

56
00:06:17,440 --> 00:06:22,068
We iterate backwards, from the end of 
the string. I've used a slice here, 

57
00:06:22,068 --> 00:06:26,400
but you could also use the reversed function.
The body of this for loop will be the same as the 

58
00:06:26,400 --> 00:06:30,659
previous one, so I'll copy and paste the code.

59
00:06:34,836 --> 00:06:37,250
Run the program again

60
00:06:38,480 --> 00:06:42,059
and we can see the characters that strip 
removed from the end of the string. 

61
00:06:42,160 --> 00:06:46,539
That might not be the behaviour you expected, 
but that's how the strip methods work. 

62
00:06:46,640 --> 00:06:49,680
You may have expected the entire 
string to be stripped from the start, 

63
00:06:49,680 --> 00:06:54,160
or end, of the string in first.
That's not what the strip methods do, 

64
00:06:54,160 --> 00:06:58,600
but we'll have a look at how we can 
achieve that. See you in the next video.

