1 00:00:07,900 --> 00:00:12,900 Welcome back. Let's see some useful methods on strings. 2 00:00:12,900 --> 00:00:17,130 Remember that none of these methods can ever change or mutate the original string, 3 00:00:17,130 --> 00:00:21,105 they can only produce a new one based on the old one. 4 00:00:21,105 --> 00:00:26,265 First, let's learn about the upper and lower methods. 5 00:00:26,265 --> 00:00:28,215 I'll show this in Codelens. 6 00:00:28,215 --> 00:00:33,765 We create a string s that is bound to Hello, World. 7 00:00:33,765 --> 00:00:38,160 If I copy the upper method on it, 8 00:00:38,160 --> 00:00:41,590 all of the letters become capital letters. 9 00:00:41,960 --> 00:00:45,990 So, we get HELLO, WORLD in capitals in the output. 10 00:00:45,990 --> 00:00:48,165 But notice that SS hasn't changed, 11 00:00:48,165 --> 00:00:50,760 it's still has the small letters. 12 00:00:50,760 --> 00:00:54,510 I can also call the lower method which will 13 00:00:54,510 --> 00:00:57,945 make the capital H and the capital W be small letters. 14 00:00:57,945 --> 00:01:02,665 In this case, I'm assigning it to a new variable called tt. 15 00:01:02,665 --> 00:01:06,030 That's what the tt equals and line four does. 16 00:01:06,030 --> 00:01:07,935 I can print that out, 17 00:01:07,935 --> 00:01:11,055 and again s has not changed, 18 00:01:11,055 --> 00:01:13,660 it still has it's original value. 19 00:01:19,100 --> 00:01:24,430 Next, I'll show you count, strip and replace. 20 00:01:25,310 --> 00:01:29,625 In this example, Hello World, 21 00:01:29,625 --> 00:01:35,705 has a bunch of spaces at the beginning and some more at the end. 22 00:01:35,705 --> 00:01:41,755 The count method works similarly to the way count works on lists. 23 00:01:41,755 --> 00:01:47,175 You specify as an argument what value to look for, 24 00:01:47,175 --> 00:01:49,200 in this case, a letter. 25 00:01:49,200 --> 00:01:53,370 We're going to look for L. It appears three times. 26 00:01:53,370 --> 00:01:56,900 So, the count method returns the value three and 27 00:01:56,900 --> 00:02:01,410 we're assigning it on line three to the variable els. 28 00:02:05,000 --> 00:02:09,735 Now, els has the value three, we can print that out. 29 00:02:09,735 --> 00:02:13,510 Line six is a little bit more complicated. 30 00:02:13,510 --> 00:02:17,470 Here, we've got something that's going to get printed out, 31 00:02:17,470 --> 00:02:20,530 nothing's going to change with our variables, 32 00:02:20,530 --> 00:02:26,180 but the thing that's getting printed out requires a little sleuthing to understand. 33 00:02:26,180 --> 00:02:32,370 So, we've got the plus operator twice and so, 34 00:02:32,370 --> 00:02:35,415 we're going to have three different strings, 35 00:02:35,415 --> 00:02:40,180 one, two and a third here. 36 00:02:40,180 --> 00:02:42,515 We're going to put them all together. 37 00:02:42,515 --> 00:02:45,695 So, we're going to get star, star, 38 00:02:45,695 --> 00:02:50,490 star and we're going to have star, 39 00:02:50,490 --> 00:02:52,810 star, star at the end. 40 00:02:56,120 --> 00:03:04,085 What we get in the middle is whatever is returned by calling the strip method on ss. 41 00:03:04,085 --> 00:03:06,555 What the strip method does, 42 00:03:06,555 --> 00:03:08,325 is it looks at a string, 43 00:03:08,325 --> 00:03:12,580 and it looks for any whitespace at the beginning and whitespace at 44 00:03:12,580 --> 00:03:17,375 the end and it gets rid of it leaving only the characters that are in the middle. 45 00:03:17,375 --> 00:03:18,945 Whitespace in the middle, 46 00:03:18,945 --> 00:03:21,360 that's okay, that does not get removed. 47 00:03:21,360 --> 00:03:24,610 So, we get Hello. 48 00:03:32,150 --> 00:03:37,070 So, that's our new string and we're not assigning it to any variable, 49 00:03:37,070 --> 00:03:42,125 but we're just asking for it to be put into the output window. 50 00:03:42,125 --> 00:03:48,420 So, we get Hello, World with the stars instead of the spaces in our output window. 51 00:03:50,440 --> 00:03:57,035 On line eight, we are calling the replace method. 52 00:03:57,035 --> 00:04:01,385 So, that's going to look for any place where the letter O 53 00:04:01,385 --> 00:04:06,210 appears and it's going to put in star, star, star instead. 54 00:04:06,210 --> 00:04:09,840 Now, strings can never be mutated. 55 00:04:09,840 --> 00:04:11,505 So, what's going to happen is, we're going to get 56 00:04:11,505 --> 00:04:16,270 a new string that has those replacements in it. 57 00:04:16,310 --> 00:04:21,450 That's going to be bound to our variable news. 58 00:04:25,670 --> 00:04:32,249 So, here, we can see that the O is replaced by three stars, 59 00:04:32,249 --> 00:04:34,830 this O is also replaced by three stars, 60 00:04:34,830 --> 00:04:41,890 but still this string that ss was bound to, it is unchanged. 61 00:04:44,600 --> 00:04:47,140 We can print that out. 62 00:04:47,140 --> 00:04:50,640 News has these whitespace at the beginning, 63 00:04:50,640 --> 00:04:54,010 so that shows up in the Output window. 64 00:05:00,020 --> 00:05:03,510 As with other pages in this text, 65 00:05:03,510 --> 00:05:06,699 there are more useful exercises, 66 00:05:06,699 --> 00:05:09,340 I'm not going to go through them all, 67 00:05:09,340 --> 00:05:15,640 and I do want to do one here that I think is particularly illuminating. 68 00:05:15,640 --> 00:05:21,810 So, here, s is bound to a string Python rocks, 69 00:05:21,810 --> 00:05:25,359 and we're going to print out this complicated expression. 70 00:05:25,359 --> 00:05:30,940 So, you have to get used to kind of breaking these big expressions into parts, 71 00:05:30,940 --> 00:05:35,620 and this star operator, if I said, 72 00:05:35,730 --> 00:05:42,785 code X times four, 73 00:05:42,785 --> 00:05:53,025 that would give me a string X X X X. 74 00:05:53,025 --> 00:06:00,469 That would just repeats the string however many times you say when you're multiplying. 75 00:06:00,469 --> 00:06:06,980 So, we really need to have the thing to the left of the star resolve to 76 00:06:06,980 --> 00:06:13,555 some character string and the thing to the right of the star to resolve to a number. 77 00:06:13,555 --> 00:06:15,360 That's what they're going to do. 78 00:06:15,360 --> 00:06:17,565 S square bracket one, 79 00:06:17,565 --> 00:06:20,880 where we have position zero, position one. 80 00:06:20,880 --> 00:06:25,079 So, s square bracket one is y, 81 00:06:25,079 --> 00:06:28,660 and s.index of n that says, 82 00:06:28,660 --> 00:06:35,160 what position can you find the letter n in this string s. Well, 83 00:06:35,160 --> 00:06:41,020 zero, one, two, three, four, five. 84 00:06:41,300 --> 00:06:45,180 So, it's the letter y times 85 00:06:45,180 --> 00:06:49,550 five and when we print it out we get it without the quotes around it, 86 00:06:49,550 --> 00:06:58,660 so sure enough it's the letter A or answer A which is yyyy. 87 00:06:59,090 --> 00:07:03,525 So, to summarize what we've learned in this segment, 88 00:07:03,525 --> 00:07:06,150 all methods on strings are non-mutating. 89 00:07:06,150 --> 00:07:08,325 They leave the original string alone. 90 00:07:08,325 --> 00:07:10,410 They generate new strings. 91 00:07:10,410 --> 00:07:15,340 So, if you want to keep changing a string that's bound to a 92 00:07:15,340 --> 00:07:17,800 variable you'll have to make a new version 93 00:07:17,800 --> 00:07:20,960 of the string and reassign it to that same variable name. 94 00:07:20,960 --> 00:07:24,890 So, the methods you've seen here are.upper and.lower 95 00:07:24,890 --> 00:07:30,910 to make the string all uppercase or all lowercase letters,.count to 96 00:07:30,910 --> 00:07:34,970 find how many occurrences of some substring occur in the larger 97 00:07:34,970 --> 00:07:40,565 string,.index to find the first position in the string 98 00:07:40,565 --> 00:07:44,380 where the substring can be found,.strip 99 00:07:44,380 --> 00:07:48,380 gets rid of all the whitespace at the beginning and the end of the string and 100 00:07:48,380 --> 00:07:51,050 that replaces all occurrences of 101 00:07:51,050 --> 00:07:57,060 some substring with a different substring. Will see you next time.