Python

Methods in Strings in Python

AKA.DM 2024. 9. 24. 07:35
반응형

    Methods are various functions implemented on data structures. The first method we will learn in this context is the len method.

    The Len method is called the abbreviation of the word lenght. It gives the length information of the objects we will put in it.

    Spcae in strings are counted as characters. 

 

 

    Startwith() method takes a value in it. And based on the value it receives, it asks Python the following question : "Does the string start with the specified value?" It returns True if the answer is true and False if fales.

    For example, there is a variable named web adress below. It contains the phrase "www.datasience.com". 

    We ask the following question to the variable with the startwith() method "Does the web adress start with www?"

 

    The output is True because the answer is correct.

 

    Let's also try to output as "false": Does the web address start with com?

    The output is False because the anwer is incorrect .

 

    In Python, the vlaues of an object are reached by its index number.

    Index numbers start from zeor and continue increasing. 

    For example, the dot character below is in the 3rd index. Again, the "t" character is in the 6th index.

 

    Startswith method can also include start and end  index.

    We can search accroding to these indexes and ask question to Python.

    For example, we ask below : Does the value between the 4th and 9th index of the "web adress" variable begin wiht the word "data"?

    Note : The value we wirte to the ending index is not inclueded.

    In the example it will be up to the 8th index. 8 not included. 

 

 

    The endswith() method is very similar to the startswith() method.

    And based on the value it receives, it asks Python the following question : "Does the stirng end with the specified value?"
    It returns True if the answer is correct, and False if incorrect.

    We got the output True because it was correct. 

 

 

    As in the startswith() method, we can enter the start and end parameters in the endswith() method.

    For example, we aske below : Does the value in the variable end as "cial" between the 3rd and 10th indexes?

    Note : The parameter we write to the ending is not included. We said up to ten below. Ten not inclueded.

반응형

'Python' 카테고리의 다른 글

Methods in String in Python-3  (0) 2024.09.26
Methods in Strings in Python 2  (0) 2024.09.25
Boolean Logic Expression in Python  (0) 2024.09.21
Eascape Sequence Opertations in Python  (0) 2024.09.20
Python Print Function  (0) 2024.09.19