Welcome back. Let's see some useful methods on strings.
Remember that none of these methods can ever change or mutate the original string,
they can only produce a new one based on the old one.
First, let's learn about the upper and lower methods.
I'll show this in Codelens.
We create a string s that is bound to Hello, World.
If I copy the upper method on it,
all of the letters become capital letters.
So, we get HELLO, WORLD in capitals in the output.
But notice that SS hasn't changed,
it's still has the small letters.
I can also call the lower method which will
make the capital H and the capital W be small letters.
In this case, I'm assigning it to a new variable called tt.
That's what the tt equals and line four does.
I can print that out,
and again s has not changed,
it still has it's original value.
Next, I'll show you count, strip and replace.
In this example, Hello World,
has a bunch of spaces at the beginning and some more at the end.
The count method works similarly to the way count works on lists.
You specify as an argument what value to look for,
in this case, a letter.
We're going to look for L. It appears three times.
So, the count method returns the value three and
we're assigning it on line three to the variable els.
Now, els has the value three, we can print that out.
Line six is a little bit more complicated.
Here, we've got something that's going to get printed out,
nothing's going to change with our variables,
but the thing that's getting printed out requires a little sleuthing to understand.
So, we've got the plus operator twice and so,
we're going to have three different strings,
one, two and a third here.
We're going to put them all together.
So, we're going to get star, star,
star and we're going to have star,
star, star at the end.
What we get in the middle is whatever is returned by calling the strip method on ss.
What the strip method does,
is it looks at a string,
and it looks for any whitespace at the beginning and whitespace at
the end and it gets rid of it leaving only the characters that are in the middle.
Whitespace in the middle,
that's okay, that does not get removed.
So, we get Hello.
So, that's our new string and we're not assigning it to any variable,
but we're just asking for it to be put into the output window.
So, we get Hello, World with the stars instead of the spaces in our output window.
On line eight, we are calling the replace method.
So, that's going to look for any place where the letter O
appears and it's going to put in star, star, star instead.
Now, strings can never be mutated.
So, what's going to happen is, we're going to get
a new string that has those replacements in it.
That's going to be bound to our variable news.
So, here, we can see that the O is replaced by three stars,
this O is also replaced by three stars,
but still this string that ss was bound to, it is unchanged.
We can print that out.
News has these whitespace at the beginning,
so that shows up in the Output window.
As with other pages in this text,
there are more useful exercises,
I'm not going to go through them all,
and I do want to do one here that I think is particularly illuminating.
So, here, s is bound to a string Python rocks,
and we're going to print out this complicated expression.
So, you have to get used to kind of breaking these big expressions into parts,
and this star operator, if I said,
code X times four,
that would give me a string X X X X.
That would just repeats the string however many times you say when you're multiplying.
So, we really need to have the thing to the left of the star resolve to
some character string and the thing to the right of the star to resolve to a number.
That's what they're going to do.
S square bracket one,
where we have position zero, position one.
So, s square bracket one is y,
and s.index of n that says,
what position can you find the letter n in this string s. Well,
zero, one, two, three, four, five.
So, it's the letter y times
five and when we print it out we get it without the quotes around it,
so sure enough it's the letter A or answer A which is yyyy.
So, to summarize what we've learned in this segment,
all methods on strings are non-mutating.
They leave the original string alone.
They generate new strings.
So, if you want to keep changing a string that's bound to a
variable you'll have to make a new version
of the string and reassign it to that same variable name.
So, the methods you've seen here are.upper and.lower
to make the string all uppercase or all lowercase letters,.count to
find how many occurrences of some substring occur in the larger
string,.index to find the first position in the string
where the substring can be found,.strip
gets rid of all the whitespace at the beginning and the end of the string and
that replaces all occurrences of
some substring with a different substring. Will see you next time.