1
00:00:05,360 --> 00:00:08,615
Alright, how can you get the behaviour 
that you might have expected? 

2
00:00:08,720 --> 00:00:13,470
How can you remove one string from the 
start (or end) of another, if it's present? 

3
00:00:13,680 --> 00:00:19,439
If you're writing code for Python 3.9, two 
new string methods were added by PEP 616. 

4
00:00:19,600 --> 00:00:22,568
These are the removeprefix 
and removesuffix methods. 

5
00:00:22,720 --> 00:00:28,134
I'll quickly demonstrate them, but be 
aware that they were added in Python 3.9. 

6
00:00:30,170 --> 00:00:35,120
If you're using Python version 3.8 or 
earlier, this next bit of code won't work. 

7
00:00:35,120 --> 00:00:39,760
The removeprefix and removesuffix 
methods were added in Python 3.9. 

8
00:00:39,760 --> 00:00:44,936
I'll show a more generic way to do the same 
thing, after the demonstration of these 2 methods. 

9
00:00:46,480 --> 00:00:50,000
Back in IntelliJ
I'll add the code after our existing code, 

10
00:00:50,000 --> 00:00:55,334
in stripping.py. I'll print a row of 
asterisks again, to separate the output: 

11
00:01:37,120 --> 00:01:40,560
Run the program, and check the 
last two lines of the output. 

12
00:01:40,560 --> 00:01:45,600
"Twas", with the apostrophe, has been removed 
from the start of the first line, and "toves" 

13
00:01:45,600 --> 00:01:50,640
has been removed from the end of the last line.
As I said, these new methods are only available 

14
00:01:50,640 --> 00:01:55,441
from Python 3.9 onwards. If you want 
your code to run with earlier versions, 

15
00:01:55,441 --> 00:01:59,504
it's quite easy to create functions that 
perform the same job as these new methods. 

16
00:01:59,840 --> 00:02:03,755
In fact, sample functions 
have been provided in PEP 616. 

17
00:02:04,080 --> 00:02:06,531
I'll define the functions 
at the start of our code: 

18
00:03:40,960 --> 00:03:44,640
Ok, we use the startswith and endswith 
methods, to check that the string we 

19
00:03:44,640 --> 00:03:49,164
want to remove really is at the start (or 
end) of the string we're removing it from. 

20
00:03:49,280 --> 00:03:53,906
The functions then use slicing to return 
the string, without the prefix or suffix. 

21
00:03:54,080 --> 00:03:57,199
To test them, I'll add the 
code at the end of the file. 

22
00:03:57,280 --> 00:04:01,873
If you're not using Python 3.9 or 
later, the existing code will crash. 

23
00:04:02,000 --> 00:04:05,939
So I'll comment those lines out 
first, and call our functions instead: 

24
00:04:20,160 --> 00:04:26,681
That's the removeprefix function. I'll do 
the same for the code to remove the suffix: 

25
00:04:39,120 --> 00:04:44,720
Run the program:
and we get the same results. 

26
00:04:44,800 --> 00:04:50,368
"Twas" has been removed in the first of the 
last 2 lines, and "toves" is gone from the last. 

27
00:04:50,480 --> 00:04:54,397
Ok, that's how you can remove a 
prefix or suffix from a string. 

28
00:04:54,480 --> 00:04:57,770
As you now know, that's not 
what the strip methods do. 

29
00:04:57,920 --> 00:05:02,614
They remove all from a sequence of characters, 
until a non-matching character is found. 

30
00:05:02,800 --> 00:05:08,240
That's the end of this brief digression. In the 
next few videos, we'll see some practical examples 

31
00:05:08,240 --> 00:05:12,720
of processing data from a text file.
See you in the next video.

