“Let’s learn Different Built-in Functions for String Data Type”

Sandeshgaikwad
5 min readSep 19, 2023

--

Hey! everyone Sandesh here again,In previous article we learned what is indexing and slicing
https://medium.com/@sandeshgaikwad14/enticing-slicing-a3ee9eec7dbf

Now lets start our new topic ‘string data type built in function’.

There are many built in functions in string data type, today we are going to learn few of them in this article.
We are going to get information about
1]capitalize method.
2]casefold method.
3]center method.
4]count method.
5]endswith method.
6]expandtabs method.
7]find method.
8]formating method.
9]index method.

Before looking in to the built in functions,lets quickly create variable which will be string type and will assign some value to the variable.
Lets consider subject= “mathematics”
subject is => string type variable
and “mathematics” is =>assigned value.
[IN]subject
[OUT] mathematics

1] capitalize method

This is the one of the built in function by the help of which we can converts existing strings very first character in to upper case or in to capital form.
syntax
>>> subject.capitalize()
or
>>> “mathematics”.capitalize()
Let consider in ‘mathematics’ all the characters are in lower case,and if we use capitalize method then it will returns the string with very first character as upper case.
‘Mathematics’

2] casefold method

In this method all the characters from the strings are converts to the lower case.Here on above image we seen after using capitalize ‘mathematics’ converts as ‘Mathematics’ (first character coverts to upper case).Now by using this method all the characters will converts to the lower case including that first capital letter.
syntax
>>> subject.casefold()

3] center method

In this method we can add width or the white space at the both end of the string.If we gives [5] then it will add 3 spaces at the beginning and 2 spaces at the end.
Let consider ‘mathematics’ is the 11 letters word and we wants 2 white spaces at both the side then we can put value[15] ===>>>11+2+2=15
syntax
>>> subject.center()====>>> subject.center[15]

4] count method

By using this method we can print the occurrence of the particular letter from the string.
Lets,consider ‘mathematics’ is the string and letter ‘m & t’ occurs 2 times.And built in function count is defined this.
syntax
>>> subject.count()

5] endswith method

This method defines the suffix which we are going to put in the parenthesis is true or false.If we provide suffix which is anywhere about the end of the string then output will print True. And if provided suffix is not the anywhere about the end then output will print False.
syntax
>>> subject.endswith()

6] expandtabs method

This function is assist tabs between the character,space is just a simple white space which can be denoted by ‘\t’ .The space can be 4 or 8 or anything is totally depends on the system.
syntax
>>> print(“string\t”.expandtab())

7] find method

By using this method we can find the index of the letter from the string.Even multiple characters being there in the parenthesis it only prints the index of the first letter of the substring.
syntax
>>> subject.find(tics)

it will runs the output in the form of index.
And if we provide substring out of the range then it will print -1 rather than any error.

8] format method

syntax
>>>print(‘string {} and {}’.format(x,y))

9] index method

Index method and find method is almost equal but there is a small difference between both of them.Considering find when we provide suffix that is out of range it doesn’t provide any error rather it shows output -1. And while considering index in same condition it will print value error.
syntax
>>> subject.index()

Hey guys… lets keep one thing in mind,by using these methods we just converting the values temporary.If we wants to make it permanent then we need to reassign it.

syntax
>>> subject=subject.upper()

So, I hope this article will helpful for you.Will meet in next article with remaining Built-in functions for the string data type.
Thanks!

#vevcodelab
@sandeshgaikwad

--

--