{"id":24762,"date":"2021-11-02T09:46:05","date_gmt":"2021-11-02T04:16:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=24762"},"modified":"2021-11-05T19:15:58","modified_gmt":"2021-11-05T13:45:58","slug":"python-string-count-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-count-method-with-examples\/","title":{"rendered":"Python String count() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String translate() Method with Examples<\/a> The count() method returns the number of occurrences of a specified value in the string.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n value:<\/strong> This is Required. It is a string The value to look for in the string.<\/p>\n start:<\/strong> This is optional. It is an integer. The starting point for the search. The default value is 0.<\/p>\n end:<\/strong> This is optional. It is an integer. The ending point for the search. The default value is the end of the string.<\/p>\n Return Value:<\/strong><\/p>\n The count() method returns the number of times the substring(value) appears in the given string.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Go through our tutorial and learn about various Python String Method Examples<\/a> and learn how to apply the knowledge while dealing with strings.<\/p>\n 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: …<\/p>\n
\nString count() Method in Python:<\/strong><\/p>\nstring.count(value, start, end)<\/pre>\n
Given string = \"hello this is btechgeeks.good morning btechgeeks\"\r\nGiven value = \"btechgeeks\"<\/pre>\n
The no of occurences of { btechgeeks } in a given string = 2<\/pre>\n
Given string = \"good morning btechgeeks good\"\r\nGiven value = \"o\"\r\nstart position = 1\r\nend position = 13<\/pre>\n
The no of occurences of { o } in a given string = 3<\/pre>\n
String count() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Without giving start and end positions<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello this is btechgeeks.good morning btechgeeks\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"btechgeeks\"\r\n# Pass the given value as an argument to the count() function for the given\r\n# string to count the number of times the given value occurred in a given string.\r\n# Store it in another variable.\r\nrslt_cnt = gvn_str.count(gvn_valu)\r\n# Print the count of the number of times\u00a0the given value occurred in\r\n# a given string.\r\nprint(\"The no of occurences of {\", gvn_valu, \"} in a given string = \", rslt_cnt)\r\n<\/pre>\n
The no of occurences of { btechgeeks } in a given string = 2<\/pre>\n
2)With giving start and end positions<\/h5>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"hello all hello good morning hello\"\r\n# Give the value as static input and store it in another variable.\r\ngvn_valu = \"hello\"\r\n# Give the start position as static input and store it in another variable.\r\ngvn_strtpositn = 6\r\n# Give the end position as static input and store it in another variable.\r\ngvn_endpositn = 16\r\n# Pass the given value, start, end positions as arguments to the count() function\r\n# for the given string to count the number of times the given value occurred in a\r\n# given string in the given start and end range.\r\n# Store it in another variable.\r\nrslt_cnt = gvn_str.count(gvn_valu, gvn_strtpositn, gvn_endpositn)\r\n# Print the count of the number of times\u00a0the given value occurred in\r\n# a given string.\r\nprint(\"The no of occurences of {\", gvn_valu,\r\n \"} in a given string = \", rslt_cnt)\r\n<\/pre>\n
The no of occurences of { hello } in a given string = 1<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)Without giving start and end positions<\/h5>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some Random String = \")\r\n# Give the value as user input using the input() function and store it in another variable.\r\ngvn_valu = input(\"Enter some Random String(value) = \")\r\n# Pass the given value as an argument to the count() function for the given\r\n# string to count the number of times the given value occurred in a given string.\r\n# Store it in another variable.\r\nrslt_cnt = gvn_str.count(gvn_valu)\r\n# Print the count of the number of times\u00a0the given value occurred in\r\n# a given string.\r\nprint(\"The no of occurences of {\", gvn_valu, \"} in a given string = \", rslt_cnt)\r\n<\/pre>\n
Enter some Random String = welcome to python programs to to all\r\nEnter some Random String(value) = to\r\nThe no of occurences of { to } in a given string = 3<\/pre>\n
2)With giving start and end positions<\/h5>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some Random String = \")\r\n# Give the value as user input using the input() function and store it in another variable.\r\ngvn_valu = input(\"Enter some Random String(value) = \")\r\n# Give the start position as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_strtpositn = int(input(\"Enter some random number = \"))\r\n# Give the end position as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_endpositn = int(input(\"Enter some random number = \"))\r\n# Pass the given value, start, end positions as arguments to the count() function\r\n# for the given string to count the number of times the given value occurred in a\r\n# given string in the given start and end range.\r\n# Store it in another variable.\r\nrslt_cnt = gvn_str.count(gvn_valu, gvn_strtpositn, gvn_endpositn)\r\n# Print the count of the number of times the given value occurred in\r\n# a given string.\r\nprint(\"The no of occurences of {\", gvn_valu,\r\n \"} in a given string = \", rslt_cnt)\r\n\r\n\r\n\r\n\r\n\r\n<\/pre>\n
Enter some Random String = good morning btechgeeks good \r\nEnter some Random String(value) = o\r\nEnter some random number = 1\r\nEnter some random number = 13\r\nThe no of occurences of { o } in a given string = 3<\/pre>\n
\n