Join Regular Classroom : Visit ClassroomTech

count() method in Python

This is a method in python string that count the number of times the given value occurs in the string and returns an integer value, if not present returns 0.

Syntax:

list.count(value)

Example 1:

s = “code hustle code repeat
x = s.count(“code“)
print(x)

output:

2

Explanation:

The word “count” occurs 2 times in the list so it returns 2.

Example 2:

s = “code hustle code repeat
x = s.count(“sleep“)
print(x)

Output:

0

Explanation:

The word “sleep” is not present in the given list, hence it returned 0.

You Missed
Also Checkout