{"id":3593,"date":"2022-12-01T20:19:48","date_gmt":"2022-12-01T20:19:48","guid":{"rendered":"https:\/\/gyanipandit.com\/programming\/?p=3593"},"modified":"2022-12-01T20:19:49","modified_gmt":"2022-12-01T20:19:49","slug":"python-string-count","status":"publish","type":"post","link":"https:\/\/gyanipandit.com\/programming\/python-string-count\/","title":{"rendered":"Python String count() Method With Example"},"content":{"rendered":"<p style=\"text-align: center;\">Python String count<\/p>\n<p>Now, we are going to learn about the <strong>count<\/strong> method in relation to the string. As the name says, the count method is going to help us count the number of times some string occurs in the specified string. For example, if we were to count how many times &#8216;a&#8217; occurs in the string &#8216;Gyanipandit&#8217;, then the answer would be simply 2. So, Let\u2019s have a look at an example over here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3594 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD.jpg\" alt=\"\" width=\"643\" height=\"72\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD.jpg 643w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD-300x34.jpg 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<h2 style=\"text-align: center;\">Python String count() Method<\/h2>\n<p>In the above example, you can simply see that we are calling the count method on the string, to count how many times &#8216;a&#8217; has occurred in the string. This method returns the count. The output to this program would be 2. we have directly printed the return value, but you can also assign that return value to some variable, as per the requirement.<\/p>\n<p>Also, you can count for some bigger strings as well. Have a look at the below example \u2013<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3595 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD-1.jpg\" alt=\"\" width=\"638\" height=\"73\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD-1.jpg 638w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD-1-300x34.jpg 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/p>\n<p>As you can see, in the above program, we have a comparatively bigger string, and we are looking for how many times has python occurred in the string. The answer is again 2.<\/p>\n<p>Now, Let\u2019s say that we pass such a string as an argument to the count method, which is not there in the string. In such a situation, the return value from the count method is going to be 0. Let\u2019s have a look at another program, which tries to demonstrate the same thing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-3596 size-full\" src=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD-2.jpg\" alt=\"\" width=\"641\" height=\"72\" srcset=\"https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD-2.jpg 641w, https:\/\/gyanipandit.com\/programming\/wp-content\/uploads\/2022\/10\/THE-count-METHOD-2-300x34.jpg 300w\" sizes=\"auto, (max-width: 641px) 100vw, 641px\" \/><\/p>\n<p>As you can see in the above program, we have a string, and then we are using the count method on the string. We are trying to count, how many times the string &#8216;python&#8217; occurred in the string1. We know that the string &#8216;python&#8217; is not present in the string at all, so the count method would return 0.<\/p>\n<p>You can try executing the program and observing the output.<\/p>\n<p>So, you can use the count method, whenever you require to count how many times some string has occurred.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python String count Now, we are going to learn about the count method in relation to the string. As the name says, the count method is going to help us count the number of times some string occurs in the specified string. For example, if we were to count how many times &#8216;a&#8217; occurs in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[26,27],"class_list":{"0":"post-3593","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python-tutorial","7":"tag-python","8":"tag-python-tutorial"},"_links":{"self":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/comments?post=3593"}],"version-history":[{"count":2,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3593\/revisions"}],"predecessor-version":[{"id":4166,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/posts\/3593\/revisions\/4166"}],"wp:attachment":[{"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/media?parent=3593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/categories?post=3593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gyanipandit.com\/programming\/wp-json\/wp\/v2\/tags?post=3593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}