Advertisement



< Prev
Next >



Python - String count() Method





As per the name suggests, the count() string method is used to count the total number of occurrence of a specific value in the invoked string.




Signature of count() method


count(value, start, end)


Parameters Description
value
This is a string value, which is searched for the number of occurrences in the invoked string and its total search count is returned. A non-optional attribute.

start
This is an integer value to specify the index position(zero-based index) to start the search at. An optional attribute.

end
An integer value to specify the index position(one-based index) to end the search at. An optional attribute.







When calling the count() method, we can even specify the start index(to begin the search at) and the last index(to end the search at), by passing the value of its two optional parameters i.e. start and end.

# Python - Example of count() method.



# Creating a string 
s1 = 'A good heart keeps you beautiful forever!'


# Printing the value of string 
print('String is : ', s1)


# Checking the count of lower case value 'e' in the string
# starting index to search is 0(zero-based index)
# last index to search is 0(one-based index)
count1 = s1.count('e', 0, 10)

print(count1)


# Checking the count of lower case value 'e' in the string
# starting index to search is 10(zero-based index)
# last index to search is 20(one-based index)
count2 = s1.count('e', 10, 20)


print(count2)


# Checking the count of lower case value 'e' in the string
# starting index to search is 10(zero-based index)
# last index to search is 40(one-based index)
count2 = s1.count('e', 10, 40)


print(count2)


Output is :


String is :  A good heart keeps you beautiful forever!
1
2
5





Please share this article -





< Prev
Next >
< String replace() Method
String split() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement