{"id":3688,"date":"2020-07-23T16:07:37","date_gmt":"2020-07-23T10:37:37","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=3688"},"modified":"2023-03-12T16:13:45","modified_gmt":"2023-03-12T10:43:45","slug":"python-count-words-in-string","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/","title":{"rendered":"4 Solid Ways To Count Words in a String in Python"},"content":{"rendered":"\n<p>Strings are essential data types in any programming language, including python. We need to perform many different operations, also known as string preprocessing like<strong> removing the unnecessary spaces, counting the words in a string, making the string in the same cases (uppercase or lowercase)<\/strong>. In this article, we will learn how to<strong> count words in a string in python.&nbsp;<\/strong><\/p>\n\n\n\n<p>We will learn how to count the number of words in a string. For example- We have a string-\u201d <strong>Hello, this is a string.\u201d It has five words<\/strong>. Also, we will learn how to count the frequency of a particular word in a string.&nbsp;<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#Different_Ways_in_Python_to_count_words_in_a_String\" >Different Ways in Python to count words in a String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#1_Count_Words_Using_For_loop\" >1. Count Words Using For loop-&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#2_Using_split_to_count_words_in_a_string\" >2. Using split()&nbsp;to count words in a string<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#3_Count_the_frequency_of_words_in_a_String_in_Python_using_Dictionary\" >3. Count the frequency of words in a String in Python&nbsp;using Dictionary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#4_Count_frequency_of_words_in_string_in_Python_Using_Count\" >4. Count frequency of words in string in Python&nbsp;Using Count()&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#Must_Read\" >Must Read<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#Conclusion\" >Conclusion&nbsp;<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-different-ways-in-python-to-count-words-in-a-string\"><span class=\"ez-toc-section\" id=\"Different_Ways_in_Python_to_count_words_in_a_String\"><\/span>Different Ways in Python to count words in a String<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Count Words Using For loop-&nbsp;<\/strong><\/li>\n\n\n\n<li><strong>Using split()&nbsp;to count words in a string<\/strong><\/li>\n\n\n\n<li><strong>Count frequency of words in a string using a dictionary<\/strong><\/li>\n\n\n\n<li><strong>Count frequency of words in string&nbsp;Using Count()&nbsp;<\/strong><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-1-count-words-using-for-loop\"><span class=\"ez-toc-section\" id=\"1_Count_Words_Using_For_loop\"><\/span>1. Count Words Using For loop-&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Using for loop is the<strong> na\u00efve approach<\/strong> to solve this problem. We count the number of spaces between the two characters.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;def count_words(string):\n    # Removing the spaces from start and end\n    string1=string.strip()\n    # Initializing the count from 1 because we there is no space at the last \n    count=1\n    # &lt;a href=&quot;http:\/\/www.pythonpool.com\/python-iterate-through-list\/&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;Iterating&lt;\/a&gt; through the string\n    for i in string1:\n    # If we encounter space, increment the count with 1.\n        if i==&quot; &quot;:\n            count+=1\n     \n    return count\nstring=&quot;Python is an interpreted, high-level, general-purpose programming language&quot;\nprint(&quot;'{}'&quot;.format(string),&quot;has total words:&quot;,count_words(string))\nstring2=&quot; Hi. My name is Ashwini &quot;\nprint(&quot;'{}'&quot;.format(string2),&quot;has total words:&quot;,count_words(string2))&lt;\/pre&gt;\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"654\" src=\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-17-1024x654.png\" alt=\"python count words in string\" class=\"wp-image-3689\" srcset=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-17-1024x654.png 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-17-300x192.png 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-17-768x491.png 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-17-1536x981.png 1536w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/image-17.png 1584w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Output-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'<a href=\"http:\/\/www.pythonpool.com\/is-python-compiled-interpreted-or-both\/\">'Python is an interpreted<\/a>, high-level, general-purpose programming language' has total words: 8'' \nHi. My name is Ashwini ' has total words: 5&nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-using-split-to-count-words-in-a-string\"><span class=\"ez-toc-section\" id=\"2_Using_split_to_count_words_in_a_string\"><\/span>2. Using split()&nbsp;to count words in a string<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>We can use split() function to count words in string.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef word_count(string):\n    # Here we are removing the spaces from start and end,\n    # and breaking every word whenever we encounter a space\n    # and storing them in a list. The len of the list is the\n    # total count of words.\n    return(len(string.strip().split(&quot; &quot;)))\n\nstring=&quot;Python is an interpreted, high-level, general-purpose programming language&quot;\nprint(&quot;'{}'&quot;.format(string),&quot;has total words:&quot;,count_words(string))\nstring2=&quot; Hi. My name is Ashwini &quot;\nprint(&quot;'{}'&quot;.format(string2),&quot;has total words:&quot;,word_count(string2))\n<\/pre><\/div>\n\n\n<p>Output-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">'<a href=\"http:\/\/www.pythonpool.com\/is-python-compiled-interpreted-or-both\/\">'Python is an interpreted<\/a>, high-level, general-purpose programming language' has total words: 8'' \nHi. My name is Ashwini ' has total words: 5&nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-3-count-the-frequency-of-words-in-a-string-in-python-using-dictionary\"><span class=\"ez-toc-section\" id=\"3_Count_the_frequency_of_words_in_a_String_in_Python_using_Dictionary\"><\/span>3. Count the frequency of words in a String in Python&nbsp;using Dictionary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;pre class=&quot;wp-block-syntaxhighlighter-code&quot;&gt;def wordFrequency(string):\n    # converting the string into lowercase\n    string=string.lower()\n    # Whenever we encounter a space, break the string\n    string=string.split(&quot; &quot;)\n    # Initializing a dictionary to store the frequency of words\n    word_frequency={}\n    # &lt;a href=&quot;http:\/\/www.pythonpool.com\/python-iterate-through-list\/&quot; target=&quot;_blank&quot; rel=&quot;noreferrer noopener&quot;&gt;Iterating&lt;\/a&gt; through the string\n    for i in string:\n    \n    # If the word is already in the keys, increment its frequency\n        if i in word_frequency:\n            word_frequency&#x5B;i]+=1\n            \n    # It means that this is the first occurence of the word\n        else:\n            word_frequency&#x5B;i]=1\n    return(word_frequency)\nstring=&quot;Woodchuck How much wood would a woodchuck chuck if a woodchuck could chuck wood ?&quot; \nprint(wordFrequency(string)) &lt;\/pre&gt;\n<\/pre><\/div>\n\n\n<p>Output-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'woodchuck': 3, 'how': 1, 'much': 1, 'wood': 2, 'would': 1, 'a': 2, 'chuck': 2, 'if': 1, 'could': 1, '?': 1}&nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-4-count-frequency-of-words-in-string-in-python-using-count\"><span class=\"ez-toc-section\" id=\"4_Count_frequency_of_words_in_string_in_Python_Using_Count\"><\/span>4. Count frequency of words in string in Python&nbsp;Using Count()&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Count() can be used to<strong> count the number of times a word occurs in a string<\/strong> or in other words it is used to tell the frequency of a word in a string. We just need to pass the word in the <a aria-label=\"undefined (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Argument_of_a_function\" target=\"_blank\" rel=\"noreferrer noopener\">argument<\/a>.&nbsp;&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef return_count(string,word):\n    string=string.lower() \n    # In string, what is the count that word occurs\n    return string.count(word)\n\nstring2=&quot;Peter Piper picked a peck of pickled peppers. How many pickled peppers did Peter Piper pick?&quot;\nreturn_count(string2,'piper')\n<\/pre><\/div>\n\n\n<p>Output-&nbsp;<\/p>\n\n\n\n<p>2&nbsp;<\/p>\n\n\n\n<p>If we want to know the number of times every word occurred, we can make a function for that.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nset1=set()\nstring=&quot;Woodchuck How much wood would a woodchuck chuck if a woodchuck could chuck wood ?&quot;\nstring=string.lower()\n# splitting the string  whenever we encounter a space\nstring=string.split(&quot; &quot;)\n# iterate through list-string\nfor i in string:\n    # Storing the word and its frequency in the form of tuple in a set\n    # Set is used to avoid repetition\n    set1.add((i,string.count(i)))\nprint(set1)\n<\/pre><\/div>\n\n\n<p>Output-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{('how', 1), ('would', 1), ('woodchuck', 3), ('a', 2), ('chuck', 2), ('could', 1), ('if', 1), ('?', 1), ('wood', 2), ('much', 1)}&nbsp;<\/pre>\n\n\n\n<p>If we want to know how many times a particular word occur in a string in an interval, we can use start and end parameters of count().&nbsp;<\/p>\n\n\n\n<p>For example-&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nstring=&quot;Can you can a can as a canner can can a can?&quot;\n# if you want to take cases into account remove this line\nstring=string.lower()\n# between index=8 and 17, how many times the word 'can' occurs\nprint(string.count(&quot;can&quot;,8,17))\n<\/pre><\/div>\n\n\n<p>Output-&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2&nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-must-read\"><span class=\"ez-toc-section\" id=\"Must_Read\"><\/span><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Must Read<\/mark><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"http:\/\/www.pythonpool.com\/python-lowercase\/\">How to Convert String to Lowercase in<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/square-root-in-python\/\">How to Calculate Square Root<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/python-user-input\/\">User Input | Input () Function | Keyboard Input<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.pythonpool.com\/python-book\/\">Best Book to Learn Python<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion&nbsp;<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the current era, data is very important. And as the world of <a href=\"http:\/\/www.pythonpool.com\/data-science-internship\/\">Data Science<\/a> is growing rapidly, and that too using python, data preprocessing is very important. We need to count words in a string in python to preprocess textual data and for that, the above-discussed methods are very important.&nbsp;<\/p>\n\n\n\n<p>Try to run the programs on your side and let us know if you have any queries.<\/p>\n\n\n\n<p><strong><em>Happy Coding!<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Strings are essential data types in any programming language, including python. We need to perform many different operations, also known as string preprocessing like removing &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"4 Solid Ways To Count Words in a String in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#more-3688\" aria-label=\"More on 4 Solid Ways To Count Words in a String in Python\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":3701,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[296,15],"tags":[1672,1670,1674,1677,1673,1679,1678,1676,1671,1669,1681,1675],"class_list":["post-3688","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to","category-tutorials","tag-count-number-of-words-in-a-string-python","tag-count-number-of-words-in-string-python","tag-count-repeated-words-in-a-string-python","tag-count-the-number-of-words-in-a-string-python","tag-count-words-in-a-string-python","tag-count-words-in-string-python","tag-how-to-count-the-number-of-words-in-a-string-python","tag-how-to-count-words-in-a-string-python","tag-python-count-number-of-words-in-string","tag-python-count-the-number-of-words-in-a-string","tag-python-count-words-in-string","tag-python-how-to-count-words-in-a-string","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>4 Solid Ways To Count Words in a String in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"In python, to count words in string, we can use the split() function, we can also make use of the count function and for loop.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"4 Solid Ways To Count Words in a String in Python\" \/>\n<meta property=\"og:description\" content=\"Strings are essential data types in any programming language, including python. We need to perform many different operations, also known as string\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-23T10:37:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-12T10:43:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1002\" \/>\n\t<meta property=\"og:image:height\" content=\"605\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ashwini Mandani\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashwini Mandani\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/\"},\"author\":{\"name\":\"Ashwini Mandani\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\"},\"headline\":\"4 Solid Ways To Count Words in a String in Python\",\"datePublished\":\"2020-07-23T10:37:37+00:00\",\"dateModified\":\"2023-03-12T10:43:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/\"},\"wordCount\":441,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg\",\"keywords\":[\"count number of words in a string python\",\"count number of words in string python\",\"count repeated words in a string python\",\"count the number of words in a string python\",\"count words in a string python\",\"count words in string python\",\"how to count the number of words in a string python\",\"how to count words in a string python\",\"python count number of words in string\",\"python count the number of words in a string\",\"python count words in string\",\"python how to count words in a string\"],\"articleSection\":[\"How to\",\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/\",\"name\":\"4 Solid Ways To Count Words in a String in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg\",\"datePublished\":\"2020-07-23T10:37:37+00:00\",\"dateModified\":\"2023-03-12T10:43:45+00:00\",\"description\":\"In python, to count words in string, we can use the split() function, we can also make use of the count function and for loop.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg\",\"width\":1002,\"height\":605,\"caption\":\"python count words in string\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"4 Solid Ways To Count Words in a String in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056\",\"name\":\"Ashwini Mandani\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g\",\"caption\":\"Ashwini Mandani\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"4 Solid Ways To Count Words in a String in Python - Python Pool","description":"In python, to count words in string, we can use the split() function, we can also make use of the count function and for loop.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/","og_locale":"en_US","og_type":"article","og_title":"4 Solid Ways To Count Words in a String in Python","og_description":"Strings are essential data types in any programming language, including python. We need to perform many different operations, also known as string","og_url":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/","og_site_name":"Python Pool","article_published_time":"2020-07-23T10:37:37+00:00","article_modified_time":"2023-03-12T10:43:45+00:00","og_image":[{"width":1002,"height":605,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg","type":"image\/jpeg"}],"author":"Ashwini Mandani","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Ashwini Mandani","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/"},"author":{"name":"Ashwini Mandani","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056"},"headline":"4 Solid Ways To Count Words in a String in Python","datePublished":"2020-07-23T10:37:37+00:00","dateModified":"2023-03-12T10:43:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/"},"wordCount":441,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg","keywords":["count number of words in a string python","count number of words in string python","count repeated words in a string python","count the number of words in a string python","count words in a string python","count words in string python","how to count the number of words in a string python","how to count words in a string python","python count number of words in string","python count the number of words in a string","python count words in string","python how to count words in a string"],"articleSection":["How to","Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-count-words-in-string\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/","url":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/","name":"4 Solid Ways To Count Words in a String in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg","datePublished":"2020-07-23T10:37:37+00:00","dateModified":"2023-03-12T10:43:45+00:00","description":"In python, to count words in string, we can use the split() function, we can also make use of the count function and for loop.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-count-words-in-string\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/07\/Capture-23.jpg","width":1002,"height":605,"caption":"python count words in string"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-count-words-in-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"4 Solid Ways To Count Words in a String in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/8a75fb48eb1f3dc952df65a8c28ac056","name":"Ashwini Mandani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f765ed1940ac0ae1d30ce4cb2f1452c1e83143b07354d25a42c1cd118980e269?s=96&d=wavatar&r=g","caption":"Ashwini Mandani"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=3688"}],"version-history":[{"count":11,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3688\/revisions"}],"predecessor-version":[{"id":26742,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/3688\/revisions\/26742"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/3701"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=3688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=3688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=3688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}