Sunday, 7 November 2021

len( ) vs count( ) | Python String

Python String method len( ) is used to get the length of the given string.

name = "Welcome to techies sphere"


Output: 25 


Python String method count( ) is used to get the number of occurrences of a substring in the given string.

string = "Welcome to techies sphere."

subString = "to"


Output: 1


Syntax of count( ) method:

string.count( subString, start, end )



subString - string whose count is to be found. 

start (Optional) - starting index within the string where count starts. 

end (Optional) - ending index within the string where count ends.

string = "Welcome to techies sphere, to learn and grow."

subString = "to"

print(string.count(subString, 5, 30))

Output: 1

1 comment: