In the previous article, we have discussed Python String translate() Method with Examples
String count() Method in Python:
The count() method returns the number of occurrences of a specified value in the string.
Syntax:
string.count(value, start, end)
Parameters
value: This is Required. It is a string The value to look for in the string.
start: This is optional. It is an integer. The starting point for the search. The default value is 0.
end: This is optional. It is an integer. The ending point for the search. The default value is the end of the string.
Return Value:
The count() method returns the number of times the substring(value) appears in the given string.
Examples:
Example1:
Input:
Given string = "hello this is btechgeeks.good morning btechgeeks" Given value = "btechgeeks"
Output:
The no of occurences of { btechgeeks } in a given string = 2
Example2:
Input:
Given string = "good morning btechgeeks good" Given value = "o" start position = 1 end position = 13
Output:
The no of occurences of { o } in a given string = 3
String count() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
1)Without giving start and end positions
Approach:
- Give the string as static input and store it in a variable.
- Give the value as static input and store it in another variable.
- Pass the given value as an argument to the count() function for the given string to count the number of times the given value occurred in a given string.
- Store it in another variable.
- Print the count of the number of times the given value occurred in a given string.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "hello this is btechgeeks.good morning btechgeeks" # Give the value as static input and store it in another variable. gvn_valu = "btechgeeks" # Pass the given value as an argument to the count() function for the given # string to count the number of times the given value occurred in a given string. # Store it in another variable. rslt_cnt = gvn_str.count(gvn_valu) # Print the count of the number of times the given value occurred in # a given string. print("The no of occurences of {", gvn_valu, "} in a given string = ", rslt_cnt)
Output:
The no of occurences of { btechgeeks } in a given string = 2
2)With giving start and end positions
Approach:
- Give the string as static input and store it in a variable.
- Give the value as static input and store it in another variable.
- Give the start position as static input and store it in another variable.
- Give the end position as static input and store it in another variable.
- Pass the given value, start and end positions as arguments to the count() function for the given string to count the number of times the given value occurred in a given string in the given start and end range.
- Store it in another variable.
- Print the count of the number of times the given value occurred in a given string.
- The Exit of the program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = "hello all hello good morning hello" # Give the value as static input and store it in another variable. gvn_valu = "hello" # Give the start position as static input and store it in another variable. gvn_strtpositn = 6 # Give the end position as static input and store it in another variable. gvn_endpositn = 16 # Pass the given value, start, end positions as arguments to the count() function # for the given string to count the number of times the given value occurred in a # given string in the given start and end range. # Store it in another variable. rslt_cnt = gvn_str.count(gvn_valu, gvn_strtpositn, gvn_endpositn) # Print the count of the number of times the given value occurred in # a given string. print("The no of occurences of {", gvn_valu, "} in a given string = ", rslt_cnt)
Output:
The no of occurences of { hello } in a given string = 1
Method #2: Using Built-in Functions (User Input)
1)Without giving start and end positions
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Give the value as user input using the input() function and store it in another variable.
- Pass the given value as an argument to the count() function for the given string to count the number of times the given value occurred in a given string.
- Store it in another variable.
- Print the count of the number of times the given value occurred in a given string.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some Random String = ") # Give the value as user input using the input() function and store it in another variable. gvn_valu = input("Enter some Random String(value) = ") # Pass the given value as an argument to the count() function for the given # string to count the number of times the given value occurred in a given string. # Store it in another variable. rslt_cnt = gvn_str.count(gvn_valu) # Print the count of the number of times the given value occurred in # a given string. print("The no of occurences of {", gvn_valu, "} in a given string = ", rslt_cnt)
Output:
Enter some Random String = welcome to python programs to to all Enter some Random String(value) = to The no of occurences of { to } in a given string = 3
2)With giving start and end positions
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Give the value as user input using the input() function and store it in another variable.
- Give the start position as user input using the int(input()) function and store it in another variable.
- Give the end position as user input using the int(input()) function and store it in another variable.
- Pass the given value, start and end positions as arguments to the count() function for the given string to count the number of times the given value occurred in a given string in the given start and end range.
- Store it in another variable.
- Print the count of the number of times the given value occurred in a given string.
- The Exit of the program.
Below is the implementation:
# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some Random String = ") # Give the value as user input using the input() function and store it in another variable. gvn_valu = input("Enter some Random String(value) = ") # Give the start position as user input using the int(input()) function and # store it in another variable. gvn_strtpositn = int(input("Enter some random number = ")) # Give the end position as user input using the int(input()) function and # store it in another variable. gvn_endpositn = int(input("Enter some random number = ")) # Pass the given value, start, end positions as arguments to the count() function # for the given string to count the number of times the given value occurred in a # given string in the given start and end range. # Store it in another variable. rslt_cnt = gvn_str.count(gvn_valu, gvn_strtpositn, gvn_endpositn) # Print the count of the number of times the given value occurred in # a given string. print("The no of occurences of {", gvn_valu, "} in a given string = ", rslt_cnt)
Output:
Enter some Random String = good morning btechgeeks good Enter some Random String(value) = o Enter some random number = 1 Enter some random number = 13 The no of occurences of { o } in a given string = 3
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.