1

00:00:00,300  -->  00:00:04,830
Welcome to listen 20 which roughly covers pages one hundred twenty eight to one hundred forty one of

2

00:00:04,830  -->  00:00:07,500
the automate the boring stuff with pipe on textbook.

3

00:00:07,500  -->  00:00:13,270
Just as a quick reminder you can actually view the entire book online at automate the boring stuff dotcom

4

00:00:13,430  -->  00:00:14,000
.

5

00:00:14,100  -->  00:00:19,350
So if you don't have a print copy you can just scroll down and then click on whichever chapter that

6

00:00:19,350  -->  00:00:20,160
we're going over.

7

00:00:20,160  -->  00:00:27,390
Inside this class I'm just like listen 15 covered a lot of useful list methods.

8

00:00:27,390  -->  00:00:30,230
This lesson will cover a lot of useful string methods.

9

00:00:30,390  -->  00:00:36,330
Keep in mind that unlike list methods the stream methods return a new string value rather than modify

10

00:00:36,330  -->  00:00:37,470
the string in place.

11

00:00:37,590  -->  00:00:41,620
Since strings are immutable it's impossible to modify them in place anyway.

12

00:00:42,240  -->  00:00:50,490
Let's take a look at the upper and lower string methods like have a variable like spam hello world and

13

00:00:50,520  -->  00:00:58,540
I could call that string values up or method and it returns a uppercase version of that string.

14

00:00:58,590  -->  00:01:02,250
Remember string values are immutable so they can't be modified in place.

15

00:01:02,250  -->  00:01:05,910
Spam still contains that lower case value.

16

00:01:05,910  -->  00:01:11,280
If I actually wanted to modify the variant the value in the variable spam would have to have something

17

00:01:11,280  -->  00:01:14,180
like spam equals spam.

18

00:01:14,220  -->  00:01:14,550
Upper

19

00:01:17,430  -->  00:01:24,060
and the lower method returns a version of that string in all lowercase non-leather characters like the

20

00:01:24,060  -->  00:01:30,180
space or the exclamation mark are completely unaffected by the upper and lower methods.

21

00:01:30,180  -->  00:01:33,810
These methods are helpful if you need to make a case insensitive comparison.

22

00:01:33,810  -->  00:01:38,880
For example if I was making some sort of game program and I printed out you want to play again I expected

23

00:01:38,880  -->  00:01:41,250
a yes or no answer.

24

00:01:41,850  -->  00:01:47,300
You would have some code that calls the input function and the player types in yes or no.

25

00:01:47,880  -->  00:01:54,490
Let's say I type in yes answer stores the lower case yes string.

26

00:01:54,660  -->  00:02:00,120
The player could have easily typed in capital Y E S.

27

00:02:00,360  -->  00:02:00,810
Once

28

00:02:04,140  -->  00:02:11,640
the player could have easily typed in capital Y E S into any code that we have that looks like this

29

00:02:11,640  -->  00:02:11,830
.

30

00:02:11,850  -->  00:02:15,330
If you answer he calls y e s lowercase

31

00:02:23,720  -->  00:02:31,280
it would fail because this expression evaluates to false if answer contains a capital contains an uppercase

32

00:02:31,290  -->  00:02:32,520
version of Yes.

33

00:02:32,550  -->  00:02:38,760
So instead we could have an expression that just automatically converts it to lower case before doing

34

00:02:38,760  -->  00:02:39,790
the comparison.

35

00:02:39,920  -->  00:02:47,370
So now it doesn't matter if answer contains a capital yes or maybe lower case and upper case characters

36

00:02:48,900  -->  00:02:53,460
in code will still work the same and still have something like this.

37

00:02:53,660  -->  00:03:03,330
Books still have something like this and it works with any response string values also have an is upper

38

00:03:03,330  -->  00:03:09,550
and lower method that returns a boolean value depending on if the strings are uppercase or lowercase

39

00:03:09,570  -->  00:03:10,590
.

40

00:03:10,590  -->  00:03:15,420
Let me try again with storing hello world in inside of spam.

41

00:03:15,750  -->  00:03:22,740
I can colspan is lower and this will return false because there is a letter character that's in upper

42

00:03:22,740  -->  00:03:34,440
case so I could change this to be a lowercase letter and now it turns it evaluates to true and I can

43

00:03:34,440  -->  00:03:41,860
do the same thing with his upper altar's first make this only have uppercase characters.

44

00:03:41,950  -->  00:03:47,800
Now spam is upper for return true.

45

00:03:47,850  -->  00:03:49,950
Note that if I just have the blank string

46

00:03:52,630  -->  00:03:55,740
both spam that is upper and is lower

47

00:03:58,560  -->  00:04:05,670
both are false there needs to be at least one uppercase or one lowercase character for is upper and

48

00:04:05,880  -->  00:04:08,740
lower to return TRUE respectively.

49

00:04:08,790  -->  00:04:14,580
So if I just called this loss during value like one two three four five both is lower and is upper would

50

00:04:14,580  -->  00:04:17,960
return false because there's no letters inside the string.

51

00:04:18,080  -->  00:04:23,550
Well since the upper and lower string methods themselves return strings you can call string methods

52

00:04:23,630  -->  00:04:26,280
and those return String values as well.

53

00:04:26,280  -->  00:04:29,710
Expressions that do this will look like a chain of method calls.

54

00:04:30,090  -->  00:04:39,870
So if I have the string hello and I call its upper method it returns the string capital hello and then

55

00:04:39,870  -->  00:04:46,890
on that string I could in turn call something like Is upper which then evaluates to a boolean value

56

00:04:46,890  -->  00:04:46,920
.

57

00:04:46,920  -->  00:04:54,370
So this entire expression evaluates to true B of the string hello and then we call its string method

58

00:04:54,450  -->  00:04:55,410
upper on that.

59

00:04:55,590  -->  00:05:04,230
And this returns a string itself which we call its method is oppor which then returns a boolean value

60

00:05:04,230  -->  00:05:04,370
.

61

00:05:04,380  -->  00:05:10,780
True there are several other of these string methods that begin with the word is.

62

00:05:10,860  -->  00:05:16,950
These methods return a boolean value that describes the nature of the string we have is Alpha that returns

63

00:05:16,950  -->  00:05:20,910
true if the string consists of only letters and it's not blank.

64

00:05:20,940  -->  00:05:26,790
Is Al name which returns true if the string only consists of letters and numbers that is it consists

65

00:05:26,790  -->  00:05:27,000
of.

66

00:05:27,000  -->  00:05:31,870
Alphanumerics is decimal which returns true if there are only numeric characters.

67

00:05:32,010  -->  00:05:37,860
Is space that returns true if there is only space characters and is title that returns true if the string

68

00:05:37,860  -->  00:05:43,240
consist of words that begin with uppercase letters followed by only lower case letters.

69

00:05:43,320  -->  00:05:47,430
So I'll type all of these out into the interactive shell we have.

70

00:05:47,430  -->  00:05:54,270
The string is alpha and this will return true because it only consists of letters.

71

00:05:54,320  -->  00:05:55,590
But if I have something like.

72

00:05:55,720  -->  00:06:02,940
Hello 2:59 is off a wrong turn return false because this one two three means that it's not just alphabetical

73

00:06:02,940  -->  00:06:04,060
characters.

74

00:06:04,920  -->  00:06:11,520
Whereas if I have hello One two three and I call is l salnum which is Fern's true.

75

00:06:11,520  -->  00:06:17,150
If there is only alphanumerics that is letters and numbers only then that returns true.

76

00:06:17,360  -->  00:06:24,990
I have something like 1 to 3 and call is decimal on it and that returns true or if I have just a bunch

77

00:06:24,990  -->  00:06:29,440
of empty space I could call it space.

78

00:06:29,550  -->  00:06:32,190
I would say yes there is nothing but empty space.

79

00:06:32,210  -->  00:06:34,560
Whitespace characters inside the string.

80

00:06:34,750  -->  00:06:39,160
Although see I had something like hello world and cold space on that.

81

00:06:39,210  -->  00:06:43,660
That would return false because these are not space characters.

82

00:06:43,680  -->  00:06:47,610
However if I had a if I had an expression.

83

00:06:47,610  -->  00:06:51,030
Sort of like where is that space.

84

00:06:51,070  -->  00:06:54,020
0 1 2 3 4 5.

85

00:06:54,190  -->  00:07:02,910
I could call it space on that and that would return true because this index returns that single space

86

00:07:03,060  -->  00:07:10,810
from the middle in between Hello world and then is title is kind of crazy looking.

87

00:07:11,040  -->  00:07:20,550
It will turn true only if all of the words inside that string begin with a uppercase character and have

88

00:07:20,550  -->  00:07:25,950
nothing but lowercase characters in them.

89

00:07:27,370  -->  00:07:32,230
There is also a corresponding title string method.

90

00:07:33,600  -->  00:07:35,620
Just like there is lower and upper.

91

00:07:36,060  -->  00:07:38,180
So that's a lot of different string methods.

92

00:07:38,280  -->  00:07:40,100
You don't have to memorize all of them.

93

00:07:40,140  -->  00:07:44,660
Just remember that they exist and you can google for them later.

94

00:07:44,730  -->  00:07:50,820
Next the starts with an ends with methods return true if a string value that they're called on begins

95

00:07:50,820  -->  00:07:54,330
or ends respectively with the string that's passed to the method.

96

00:07:54,570  -->  00:08:01,560
So if we had a string like hello world we could call starts with and then passed that string.

97

00:08:01,590  -->  00:08:05,750
Hello and this will return true.

98

00:08:06,000  -->  00:08:09,670
We could even make that much simpler and say does it start with the letter H.

99

00:08:09,690  -->  00:08:11,090
And that's true.

100

00:08:12,180  -->  00:08:18,860
Whereas this would return false because it does not start with L-O.

101

00:08:18,930  -->  00:08:21,220
We need that beginning H right here.

102

00:08:21,480  -->  00:08:26,660
Similarly we have an ends with method.

103

00:08:27,590  -->  00:08:33,270
Note that it starts with an S at the end and ends with an S at the end.

104

00:08:33,750  -->  00:08:37,650
So we could ask it O does it end with world exclamation point.

105

00:08:37,770  -->  00:08:39,570
And this would return true.

106

00:08:39,600  -->  00:08:41,330
Of course if we.

107

00:08:41,700  -->  00:08:49,830
Although if we didn't have that exclamation mark at the end this would return false.

108

00:08:49,980  -->  00:08:54,960
The join method is useful when you have a list of strings that need to be joined together into a single

109

00:08:54,960  -->  00:08:56,180
string value.

110

00:08:56,280  -->  00:09:01,820
The join method is called on a string gets past a list of strings and returns a string.

111

00:09:02,550  -->  00:09:03,930
So here's a common example.

112

00:09:03,930  -->  00:09:11,470
We have the string comma and we'll use that to join together a list of strings.

113

00:09:11,810  -->  00:09:17,590
Let's say cats rats bats.

114

00:09:18,210  -->  00:09:24,810
So this method call returns a string that has each of these individual strings in this list combined

115

00:09:24,810  -->  00:09:27,720
together and joined with this string.

116

00:09:27,780  -->  00:09:30,700
So a combo appears in between each of them.

117

00:09:31,160  -->  00:09:35,550
We could change this so that we have nothing that joins them just have the blank string so that they

118

00:09:35,550  -->  00:09:37,920
all just get crammed together already.

119

00:09:37,950  -->  00:09:44,630
Or perhaps we could have a single space in them so that there is a single space there.

120

00:09:45,110  -->  00:09:52,770
Or even say to newline characters have Slash selection so that way when we passed this twig print function

121

00:09:52,770  -->  00:10:00,660
call it will print it out with a new line here and then another new line here to have a gap in between

122

00:10:00,690  -->  00:10:02,930
each of the words.

123

00:10:03,030  -->  00:10:05,250
The split method does the opposite.

124

00:10:05,250  -->  00:10:10,930
It's cold on a string value and returns a list of strings that have the string.

125

00:10:10,950  -->  00:10:19,890
My name is Simon and when I call split on it it returns a list and inside this list or all the individual

126

00:10:19,890  -->  00:10:27,890
words from inside that string it splits it on whitespace characters it we can have it split on other

127

00:10:27,890  -->  00:10:34,310
characters as well just by passing a different value to the split method say I wanted it to split on

128

00:10:34,490  -->  00:10:37,400
the letter M instead of on spaces.

129

00:10:37,400  -->  00:10:43,850
Now this list will have my number as the first string and then it splits.

130

00:10:43,850  -->  00:10:51,950
It cuts out this M set everything up to the next AM is in the second string and then the rest of that

131

00:10:51,950  -->  00:10:53,390
is the third string.

132

00:10:53,440  -->  00:11:00,980
This is just like how it used to go up to the first space character and then have the next string be

133

00:11:01,070  -->  00:11:02,840
up to the next space character.

134

00:11:03,320  -->  00:11:06,500
So you can split on anything that you want.

135

00:11:06,500  -->  00:11:12,440
The are just an L just string methods or turny padded version of a string that they're called on with

136

00:11:12,440  -->  00:11:17,000
spaces inserted to left justify or right justify the text.

137

00:11:17,000  -->  00:11:21,670
So the first argument to both methods is the integer length for the justified string.

138

00:11:21,920  -->  00:11:25,510
So if I have the string Hello I can write justify it.

139

00:11:25,940  -->  00:11:29,150
Let's say I want the total length of the return String to be 10.

140

00:11:29,150  -->  00:11:35,390
So in this case Python inserts as many space characters as needed to make the total length of this string

141

00:11:37,460  -->  00:11:39,360
to be 10.

142

00:11:40,010  -->  00:11:44,780
And I can do the same thing with the L just method to less left justified.

143

00:11:44,780  -->  00:11:47,360
This will add white space padding to the hand

144

00:11:50,300  -->  00:11:56,960
and I can try passing a different value to justify it with 20 spaces and an optional second argument

145

00:11:56,960  -->  00:12:03,050
can be passed you are just an L just to specify a difference Fille character other than a space.

146

00:12:03,200  -->  00:12:09,490
So if I had hello that are just and I wanted it to be 20 characters long.

147

00:12:09,500  -->  00:12:17,000
And then also have asterisks instead of spaces I could create a string easily with stars at the front

148

00:12:17,000  -->  00:12:27,020
of it or say I wanted the text Hello to the left justified with 25 characters and then dashes instead

149

00:12:27,020  -->  00:12:27,890
.

150

00:12:27,890  -->  00:12:33,710
And there's also a center method that works just like l just in are just what centrists the text rather

151

00:12:33,710  -->  00:12:36,780
than justifying it to the left or right.

152

00:12:37,270  -->  00:12:38,040
Second half.

153

00:12:38,390  -->  00:12:39,290
Hello.

154

00:12:39,440  -->  00:12:45,970
And then centered so that the entire string is 20 characters long with the text in the center.

155

00:12:45,980  -->  00:12:51,530
Or I could have maybe equal sign as the fill character instead.

156

00:12:51,530  -->  00:12:59,770
This is kind of nice because you could have a variable that contains something like name equals Al.

157

00:13:02,940  -->  00:13:11,060
Or it could change that to be a very long name like Wendell the law of the law.

158

00:13:11,720  -->  00:13:17,030
And that same code will put that name in the center with as many equal signs on either side as necessary

159

00:13:17,030  -->  00:13:18,260
.

160

00:13:18,310  -->  00:13:20,000
It uses the same code

161

00:13:23,630  -->  00:13:29,150
sometimes you might want to strip off whitespace characters like spaces or tabs or new lines from the

162

00:13:29,150  -->  00:13:32,900
left side or the right side or both sides of the string going back.

163

00:13:32,900  -->  00:13:40,160
If we had this hello that are just we had too much space on the on the left side of this when we want

164

00:13:40,160  -->  00:13:47,480
to get rid of that we can call the strip string method to remove that.

165

00:13:47,540  -->  00:13:58,590
So let's say I have that in spam and I could call strip on that string to remove that whitespace.

166

00:13:58,670  -->  00:14:01,330
This doesn't modify spam in place.

167

00:14:01,610  -->  00:14:03,250
Spam still has all of that in there.

168

00:14:03,260  -->  00:14:05,690
This just returns a brand new string.

169

00:14:05,990  -->  00:14:12,980
So if I wanted to change it I would have spam equals spam got strip No remove whitespace from either

170

00:14:12,980  -->  00:14:13,520
side

171

00:14:17,240  -->  00:14:19,050
of the string.

172

00:14:20,620  -->  00:14:24,620
Let's say we only want to move remove the whitespace from the left side of the string we could call

173

00:14:24,650  -->  00:14:32,150
L strip or we could call our strip turr just remove whitespace from the right side of the string.

174

00:14:32,150  -->  00:14:38,000
We could also pass the strip methods a string with characters that we want to remove instead of whitespace

175

00:14:38,000  -->  00:14:41,600
.

176

00:14:41,870  -->  00:14:51,610
So if I had a variable like spam spam Bacons spam eggs spam spam I could Colstrip on that and pass it

177

00:14:51,600  -->  00:14:51,650
.

178

00:14:51,680  -->  00:14:54,880
The letters KMP capital S..

179

00:14:54,950  -->  00:15:01,970
I can have this in any order I'm just specifying letters not an actual word and this or move all the

180

00:15:01,970  -->  00:15:09,360
capital S is PS A's and M's from either side of the string and return that new string guy.

181

00:15:09,440  -->  00:15:11,680
There is however still spam in the middle.

182

00:15:11,840  -->  00:15:17,600
That's just because it removed those letters up to the first character that wasn't passed to strict

183

00:15:17,600  -->  00:15:18,410
method.

184

00:15:18,890  -->  00:15:22,600
And then the same on the inside it just went down to here.

185

00:15:22,700  -->  00:15:30,560
So this is the string that was returned in the replace string method takes two arguments a string to

186

00:15:30,560  -->  00:15:33,860
look for and a string to replace it with.

187

00:15:33,890  -->  00:15:42,140
So if I had a string like hello they're stored in spam I could code replace and replace every e with

188

00:15:42,500  -->  00:15:43,430
x y z

189

00:15:47,430  -->  00:15:50,260
and that's all the string methods that will cover in this lesson.

190

00:15:50,270  -->  00:15:51,770
Let's go over one more thing.

191

00:15:51,770  -->  00:15:58,370
The paperclip module has copy and paste functions that can send text to and receive text from your computer's

192

00:15:58,370  -->  00:16:03,260
clipboard so sending the output of your program to the clipboard will make it really easy to paste it

193

00:16:03,260  -->  00:16:07,730
to an email or to a word processor software or some other program.

194

00:16:08,210  -->  00:16:11,480
The paperclip doesn't come with Python to install it.

195

00:16:11,480  -->  00:16:16,500
Follow the directions for installing third party modules with the pip tool in Appendix A.

196

00:16:16,970  -->  00:16:19,690
I can show you very quickly how to do it on Windows.

197

00:16:19,700  -->  00:16:25,100
I'm just going to hit window key R and run CM D.

198

00:16:26,620  -->  00:16:28,790
I'll just go to the directory.

199

00:16:29,180  -->  00:16:37,400
So change directory to where Python is installed and go to its scripts folder that has this program

200

00:16:37,560  -->  00:16:38,800
pip dot exe.

201

00:16:39,160  -->  00:16:45,410
and I'll just type pip dot exe install paperclip and this will automatically download and install it

202

00:16:45,470  -->  00:16:46,820
onto your system.

203

00:16:46,880  -->  00:16:48,360
In this case I've already have it.

204

00:16:48,380  -->  00:16:52,890
I already have it installed so it says you don't need to install it again.

205

00:16:54,110  -->  00:16:57,230
So after installing it you can check to see if the install worked.

206

00:16:57,260  -->  00:16:59,260
Just trying to import paperclip.

207

00:16:59,260  -->  00:17:06,320
And if nothing shows up then that means it's successfully been installed and paperclip has a copy and

208

00:17:06,560  -->  00:17:11,110
paste functions that can copy text to the clipboard.

209

00:17:11,120  -->  00:17:21,050
So say I can copy this string hello with lots of exclamation marks and now when I try to paste text

210

00:17:22,850  -->  00:17:26,430
I can see that this text was indeed copied to the clipboard.

211

00:17:26,610  -->  00:17:28,970
Or I can call paperclips.

212

00:17:29,120  -->  00:17:33,320
Paste function and that will return the text that's already on the clipboard.

213

00:17:33,380  -->  00:17:40,190
So if I wanted to get that as a string value I can just call paperclips up paste to recap.

214

00:17:40,190  -->  00:17:48,540
Upper and lower return in uppercase or lowercase version of the string is upper is lower is alpha.

215

00:17:48,560  -->  00:17:52,360
Is Al knowm is decimal is space is title.

216

00:17:52,640  -->  00:17:58,310
All of those is string for methods return true or false depending on if the string is that respective

217

00:17:58,310  -->  00:18:01,610
kind of string starts with and ends with.

218

00:18:01,610  -->  00:18:03,280
Also return balls.

219

00:18:03,680  -->  00:18:08,760
The join method will return a string that combines the strings from a list.

220

00:18:08,870  -->  00:18:15,800
The split method will return a list of strings are just eldest and center will return a string that's

221

00:18:15,800  -->  00:18:22,280
padded with spaces or some other character that you specify the strip are strip and L strip methods

222

00:18:22,280  -->  00:18:25,880
will return a string with the whitespace stripped off from the sides.

223

00:18:25,880  -->  00:18:30,800
Replace will replace all occurrences of the first string argument with the second string argument that

224

00:18:30,800  -->  00:18:36,860
you pass it and the paperclip module has copy and paste functions so that your programs can use the

225

00:18:36,860  -->  00:18:38,730
computer's clipboard.
