Advertisement
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. |
# Python - Example of count() method.
# Creating a string
s1 = 'Rock and Roll'
# Printing the value of string
print('String is : ', s1)
# Checking the count of value 'Roll' in the string
count1 = s1.count('Roll')
print('Count of Roll in ', s1, ' : ', count1)
# Checking the count of value 'o' in the string
count2 = s1.count('o')
print('Count of o in ', s1, ' : ', count2)
# Checking the count of value 'l' in the string
count3 = s1.count('l')
print('Count of l in ', s1, ' : ', count3)
String is : Rock and Roll
Count of Roll in Rock and Roll : 1
Count of o in Rock and Roll : 2
Count of l in Rock and Roll : 2
# Python - Example of count() method.
# Creating a string
s1 = 'LIVE and let live'
# Printing the value of string
print('String is : ', s1)
# Checking the count of lower case value 'l' in the string
count1 = s1.count('l')
print('Count of l in ', s1, ' : ', count1)
# Checking the count of upper case value 'L' in the string
count2 = s1.count('L')
print('Count of L in ', s1, ' : ', count2)
# Checking the count of lower case value 'e' in the string
count3 = s1.count('e')
print('Count of e in ', s1, ' : ', count3)
String is : LIVE and let live
Count of l in LIVE and let live : 2
Count of L in LIVE and let live : 1
Count of e in LIVE and let live : 2
Advertisement
# 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)
String is : A good heart keeps you beautiful forever!
1
2
5
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement