Menu Close

Methods count(), len(), and find()

While the strip() and format() methods were important to know for working with python Strings, these methods are also very important. This article will discuss the uses of the count(), len(), and find() methods.

count() method

The count() method in python searches iterables for references of a given term. It returns the amount of matches inside of a string, list, tuple, or any other iterable.
The syntax is as follows:

string.count(value, start, end)

If the start and end values are left empty, the method will count throughout the whole string.

Example 1:

string1 = “aaababbbbaaaabababaaabababaaaabbbbababaa”
print(string1.count(“a”))
print(string1.count(“b”))

Output:

23
17

In the example above, the code will find all instances of the letter “a” and the letter “b”.

Example 2:

string1 = “John has 1 banana. He loves eating bananas and he would be very sad if he had no more bananas to eat.”
print(string1.count(“banana”, 20, 100)

Output:

2

In the example above, the code only searches for the term “banana” from letter 20 up to letter 100.

len() method

In python, it is often useful to get the length of an iterable. For this, we use the len() method, which returns the length of a given object.
The syntax is as follows:

lenObj = len(iterable)

This is often very useful as the len() method allows a for loop to iterate, or go through, every character in a string or every object in a list.

Example 1:

string1 = “aaababbbbaaaabababaaabababaaaabbbbababaa” 
string2 = “John has 1 banana. He loves eating bananas and he would be very sad if he had no more bananas to eat.”

print(len(string1))
print(len(string2))

Output:

40
101

In the code above, it finds the length of string1 and string2, which have 40 and 101 letters respectively.

Example 2:

string1 = “John has 1 banana. He loves eating bananas and he would be very sad if he had no more bananas to eat.”
string2 = “”
vowels = [“a”, “e”, “i”, “o”, “u”]

for i in range(len(string1)):
    if string1 in vowels:
        string2 += string1

print(string2)

Output:

Jhn hs 1 bnn. H lvs tng bnns nd h wld b vry sd f h hd n mr bnns t t.

In the code above, the len() method is used in inside a for loop, which is a very common use case. It is used to loop through the entire string and add all the consonants to a new string.

find() method

Often, programmers need to find the index of a given object. Using the find() method, programmers are able to get the first occurrence of the specified value.
The syntax is as follows:

string.find(“value”, start, end)

This will return the index of the value specified, or it will return -1 if none is found. The start and end values are the start and end indexes for which it will search through. If these values are left blank, it will search the entire string.

Example 1:

myString = “Python is a very hard programming language to learn.”
print(myString.find(“language”))

Output:

34

In the code above, it was able to find the word “language” at index value 34.

Example 2:

myString = “Python is a very hard programming language to learn. Python is very popular”
print(myString.find(“python”, 50))

Output:

53

The code above searches through the string for the word “Python”, but only after index 50. This is why the first “python” is ignored.

Example 3:

myString = “Python is a very hard programming language to learn.”
print(myString.find(“java”))

Output:

-1

The method above returns -1 as there is no instance of “java” in the string above.

Posted in Embedded Programming Language, Python

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!