General Sequence Data Methods | Part I
Some of the methods that we can apply on any sequential data.
So basically Python methods are like a Python function, but it must be called on an object.
And Python also has a set of built in methods that you can use on sequential data, but note that the method is going to written new values, but they are not going to change the original data at all. So we are going to look at few examples on how we can call the methods on the given sequential data.
First I am creating a string for strsample which has a string to with that is learning is fun. So let
me first print and show you. So, the strsample contains string learning is fun.
String Methods
1) .capitalize() - So let us try to call some of the methods on the string but note here all string method returns new value. We do not change the original string. First we are going to look here is method capitalise and it is going to return the string with the first character capitalise. So let us just try this. So initially it was learning is fun and lowercase and now it is learning is fun with the upper case L.
2) .title() - Next one is title, basically capitalise the first character of each word. So if you have a sequence of values, if you have a string if you have set of strings in your data and if you want to do some of the operations like this then you can use all these methods on your string. So for example now, if you want to capitalise the first character of each word, then you can use title method in that case. So, it is going to written the values by capitalising the first character alone.
3) .swapcase() - And thus a method calls what case which is going to swap the
case of strings. So if it is a lowercase string then it is going to swap it to make it as uppercase string. So the original string had the learning is fun in all lowercase letters, but just what all the characters to uppercase.
4) .find() - And you can also find the index of a given letter, for example, if you want to particularly know the index of a given character then you can give that inside the find method. So that should be given the single quote. So the index of n is 4.
5) .count() - So if you want to count the total number of particular letter in a given string then you can use the count method. And inside the count method you can just give the desired string value. So that is one because a is present only ones. For example here is there in the word learning only once
6) .replace() - Using the method replace you will be able to replace any word with the given word. For example, you can apply or you can call that on the string and that is strsample and inside method you can just give string to be searched that is fun. So we are searching for the word fun in the strsample string and I am going to replace fun with joyful. So let me just try this. So you are getting learn is joyful. Instead of getting learn is fun which was the original string.
The below code will show all the functions that we can use for the particular variable:
0 Comments:
Post a Comment