This tutorial we will discuss about drawing macaques Python has a set of built in method that we can
use on strings and these methods can be used to manipulate strings.
So we will discuss about some of the metrics before discussing about the method.
I would like to discuss about one important point as we know the strings are immutable.
So North string method can change the original string.
It can only do it on a new string so let us check this using an example.
This is the syntax for calling a method on an object Object dot method open and close parenthesis the
connection between the method with the object is indicated by a dot written between them the dot notation
is used to access the methods so in no case the object would be let's string please don't worry much
about objects and method because I have discussed about objects method functions in detail in another
tutorial.
For now we will understand how each method can be used on the string to manipulate it.
Say for example I would like to change all of the characters in the string to uppercase.
So in that case I can use the method output and if I want to apply this method on the object.
This is the syntax is dot upper open close parenthesis.
So when this particular method is applied on the object that is a string this word can work all of the
characters do with ease let us check this in the Jupiter note book.
So we have a string s which contains this text Halloway and I want to change all of the characters in
this string to uppercase.
So we'll call the method a bird on this string s then execute the cell so all of the characters have
been converted to uppercase.
Similarly we have another method Lowood which converts all of the characters to lowercase so let's execute
this in had a word in lowercase
another method is capitalized which will capitalize capitalizes only the first letter word in the string.
So this is the method capitalize
execute.
So as you can see the word devil you in the origin of string which is in capitals has been changed to
lowercase so we have talked before that strings are immutable and the different methods that are applied
on the object do not change the original string.
Let's display these string s and see if there are any changes to the string as you can see none of the
methods could change the original string.
The next matter that we're going to talk about is count which returns the number of times a specified
value has occurred in this string.
So I want to count the number of times the letter or it occurs in this particular string so as dark
count and in the parent is we give the character at all.
So there are two O's in the string.
And here is the one from the count method.
The next method is is Lowood.
It returns a boolean value if all of the characters in the string are in lowercase so let execute this
cell.
And check point.
That's because the characters in this string are in upper and lower Keys.
If you want to find out the position of a substring in a string you can use the find mentor and independent
voices you enter the substring that you want to find in the string.
So I want to look for an oh so the defined method returns the lowest index where the substring is found.
So in the string is the substring a law is at the third position.
So
the method fine it an integer value.
Let's so line and not this up string.
Let's find the position all the substring or so as you can see it only returned the lowest index or
the substring so it.
It has returned the position or the substring or here which has an index for say for example the substring
that you want to search for in the string is not found by the method line.
In that case it returns of minus 1.
If you want to replace an all substring with a new substring you can use the method replace.
And here you need to specify two parameters.
The first parameter is for the all substring say I want to replace the substring or with the substring
E and then execute this in so very good.
The Interpreter.
Find the substring.
All it is replaced with the substring.
You can check if a string starts with a specified prefix using the start with method.
And this method returns a boolean value which is true or false.
Say for example independent this is I enter I entered H and then execute the C source and the string
Hello World starts with the letter H.
This is true.
Start with
the substring B and it should return flight
next to the index method.
Then this method returns the index or the substring if it is found in a string.
So let us find what the
spewing substring w so it is given an integer value 6.
Let us fine for it and not the substring C M so since m is not in hello world.
It has raised an exception.
Next does the split method.
The split method breaks the string at the separator which is specified in the parenthesis yard and it
returns a list of strings so for example I want to specify separated spaces and then executed using
so the method has split the string into two part one is hello and the board under the split.
Add add these specify a d separator that has been given your which is spaces.
So let us give another separator C I would like to let add the substring separator or so wherever it
finds the separator or it splits the string and then returns the sub strings in a list.
Say for example here I give another word a separate code which is not found in this string that gays
it does not split the string and it returns the string as one single element in the list.
The length method returns the length of the string and it doesn't take any parameters other than the
name of the string.
So independent assess here I I'm going to enter this string s then execute the length method includes
letters called mass spaces be added exclamation marks apostrophe use when calculating the length of
a string.
So this is about the different string method.