{"id":14897,"date":"2024-12-23T09:04:20","date_gmt":"2024-12-23T06:04:20","guid":{"rendered":"https:\/\/elzero.org\/?p=14897"},"modified":"2024-12-23T09:05:36","modified_gmt":"2024-12-23T06:05:36","slug":"python-count-number-of-words-in-a-string","status":"publish","type":"post","link":"https:\/\/elzero.org\/old\/python-count-number-of-words-in-a-string\/","title":{"rendered":"\u062d\u0633\u0627\u0628 \u0623\u0643\u062b\u0631 \u0643\u0644\u0645\u0629 \u0645\u062a\u0643\u0631\u0631\u0629 \u0641\u064a \u0627\u0644\u0646\u0635 \u0628\u062f\u0648\u0646 \u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0645\u0643\u062a\u0628\u0627\u062a"},"content":{"rendered":"<div class=\"text\">\r\n\u062d\u0633\u0627\u0628 \u0623\u0643\u062b\u0631 \u0643\u0644\u0645\u0629 \u0645\u062a\u0643\u0631\u0631\u0629 \u0641\u064a \u0627\u0644\u0646\u0635 \u0628\u062f\u0648\u0646 \u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0645\u0643\u062a\u0628\u0627\u062a\r\n<\/div>\r\n\r\n<div class=\"text\">\r\n\u0623\u0648\u0644\u0627 \u0646\u0642\u0648\u0645 \u0628\u062c\u0644\u0628 \u0627\u0644\u0646\u0635 \u0627\u0644\u0645\u0631\u0627\u062f \u062a\u062d\u0648\u064a\u0644\u0647\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Get Input From User\r\ntext = input(\"Add Your Input Here: \")\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0646\u0642\u0648\u0645 \u0628\u062c\u0644\u0628 \u0643\u0644 \u0627\u0644\u0643\u0644\u0645\u0627\u062a \u0627\u0644\u0645\u0648\u062c\u0648\u062f\u0629 \u0641\u064a \u0627\u0644\u0646\u0635 \u0648\u0648\u0636\u0639\u0647\u0627 \u062f\u0627\u062e\u0644 List\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Split The String To Elements Inside List\r\nwords = text.split()\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0646\u0642\u0648\u0645 \u0628\u0625\u0646\u0634\u0627\u0621 \u0642\u0627\u0645\u0648\u0633 Dictionary \u0644\u062a\u062e\u0632\u064a\u0646 \u0627\u0644\u0643\u0644\u0645\u0627\u062a \u0648\u0639\u062f\u062f \u062a\u0643\u0631\u0627\u0631\u0647\u0627\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Create Dictionary To Count Number Of Words\r\nword_count = {}\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0646\u0642\u0648\u0645 \u0628\u0639\u0645\u0644 Loop \u0639\u0644\u0649 \u062c\u0645\u064a\u0639 \u0627\u0644\u0643\u0644\u0645\u0627\u062a \u0627\u0644\u0645\u0648\u062c\u0648\u062f\u0629 \u062f\u0627\u062e\u0644 \u0627\u0644 List \u0648\u0625\u0636\u0627\u0641\u0629 \u0627\u0644\u0643\u0644\u0645\u0629 \u062f\u0627\u062e\u0644 \u0627\u0644\u0642\u0627\u0645\u0648\u0633 \u0648\u0628\u062c\u0627\u0646\u0628\u0647\u0627 \u0639\u062f\u062f \u0627\u0644\u0645\u0631\u0627\u062a \u0627\u0644\u062a\u064a \u062a\u0643\u0631\u0631\u062a \u0641\u064a\u0647\u0627 \u0627\u0644\u0643\u0644\u0645\u0629, \u0641\u064a \u062d\u0627\u0644\u0629 \u0627\u0644\u0643\u0644\u0645\u0629 \u0643\u0627\u0646\u062a \u0645\u0648\u062c\u0648\u062f\u0629 \u0645\u0646 \u0642\u0628\u0644 \u0633\u0646\u0642\u0648\u0645 \u0628\u0632\u064a\u0627\u062f\u0629 \u0631\u0642\u0645 \u0648\u0627\u062d\u062f \u0627\u0645\u0627 \u0644\u0648 \u0643\u0627\u0646\u062a \u0627\u0644\u0643\u0644\u0645\u0629 \u062c\u062f\u064a\u062f\u0629 \u0633\u0646\u0642\u0648\u0645 \u0628\u0648\u0636\u0639 \u0642\u064a\u0645\u062a\u0647\u0627 \u0631\u0642\u0645 \u0648\u0627\u062d\u062f, \u0627\u0642\u0631\u0623 \u0627\u0644\u062a\u0639\u0644\u064a\u0642\u0627\u062a \u0641\u064a \u0627\u0644\u0643\u0648\u062f \u0644\u0641\u0647\u0645 \u0627\u0644\u0641\u0643\u0631\u0629\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Loop On Words\r\nfor word in words:\r\n\r\n  # If The Word Exists \r\n  if word in word_count:\r\n\r\n    # Add 1 To Word Count\r\n    word_count[word] += 1\r\n\r\n  # If Word Is New\r\n  else:\r\n\r\n    # Set Word Count To 1\r\n    word_count[word] = 1\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0633\u064a\u0643\u0648\u0646 \u0627\u0644\u0642\u0627\u0645\u0648\u0633 \u0628\u0647\u0630\u0627 \u0627\u0644\u0634\u0643\u0644 \u0641\u064a \u0627\u0644\u0646\u0647\u0627\u064a\u0629 \u0625\u0630\u0627 \u0625\u0633\u062a\u0639\u0645\u0644\u0646\u0627 \u0627\u0644\u0645\u062f\u062e\u0644\u0627\u062a \u0641\u064a \u0627\u0644\u0643\u0648\u062f \u0627\u0644\u062a\u0627\u0644\u064a\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Input -> \"Elzero Elzero Web Web Elzero School School School Elzero\"\r\n{'Elzero': 4, 'Web': 2, 'School': 3}\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0625\u0646\u0634\u0627\u0621 \u0645\u062a\u063a\u064a\u0631\u0627\u062a \u0644\u062a\u062e\u0632\u064a\u0646 \u0627\u0644\u0628\u064a\u0627\u0646\u0627\u062a\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Create Variables To Store Results\r\nmost_common_word = None\r\nhighest_count = 0\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0627\u0644\u0622\u0646 \u0646\u0642\u0648\u0645 \u0628\u0639\u0645\u0644 Loop \u0639\u0644\u0649 \u0627\u0644\u0642\u0627\u0645\u0648\u0633 \u0648\u0633\u0646\u0645\u0631 \u0639\u0644\u0649 \u0643\u0644 \u0643\u0644\u0645\u0629 \u0625\u0630\u0627 \u0648\u062c\u062f\u0646\u0627 \u0639\u062f\u062f \u0645\u0631\u0627\u062a \u062a\u0643\u0631\u0627\u0631\u0647\u0627 \u0623\u0643\u062b\u0631 \u0645\u0646 \u0627\u0644\u0645\u062a\u063a\u064a\u0631 highest_count \u0633\u0646\u0639\u062a\u0628\u0631 \u0623\u0646 \u0647\u0630\u0647 \u0623\u0643\u062b\u0631 \u0643\u0644\u0645\u0629 \u0645\u0643\u0631\u0631\u0629 \u062d\u062a\u0649 \u0646\u0631\u0649 \u0647\u0646\u0627\u0643 \u0623\u0643\u062b\u0631 \u0645\u0646\u0647\u0627 \u0623\u0645 \u0644\u0627 \u062b\u0645 \u0646\u0643\u0645\u0644 \u0627\u0644 Loop \u0644\u0648 \u0648\u062c\u062f\u0646\u0627 \u0643\u0644\u0645\u0629 \u0623\u0643\u062b\u0631 \u0645\u0646\u0647\u0627 \u0641\u064a \u0627\u0644\u062a\u0643\u0631\u0627\u0631 \u0633\u0646\u0636\u0639 \u0627\u0644\u0642\u064a\u0645\u0629 \u0641\u064a highest_count \u0623\u0645\u0627 \u063a\u064a\u0631 \u0630\u0644\u0643 \u0633\u0646\u0639\u062a\u0628\u0631 \u0627\u0644\u0643\u0644\u0645\u0629 \u0627\u0644\u0633\u0627\u0628\u0642\u0629 \u0647\u064a \u0623\u0643\u062b\u0631 \u0643\u0644\u0645\u0629 \u0645\u0643\u0631\u0631\u0629 \u0648\u0647\u0643\u0630\u0627\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Loop On Dictionary Items\r\nfor word, count in word_count.items():\r\n  if count > highest_count:\r\n    most_common_word = word\r\n    highest_count = count\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0637\u0628\u0627\u0639\u0629 \u0627\u0644\u0646\u062a\u0627\u0626\u062c\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\nprint(\"Most Common Word\", most_common_word)\r\nprint(\"Number Of Repeats\", highest_count)\r\n<\/script>\r\n\r\n<div class=\"text\">\r\n\u0627\u0644\u0643\u0648\u062f \u0627\u0644\u0643\u0627\u0645\u0644\r\n<\/div>\r\n\r\n<script type=\"text\/plain\" class=\"language-python\">\r\n# Get Input From User\r\ntext = input(\"Add Your Input Here: \")\r\n\r\n# Split The String To Elements Inside List\r\nwords = text.split()\r\n\r\n# Create Dictionary To Count Number Of Words\r\nword_count = {}\r\n\r\n# Loop On Words\r\nfor word in words:\r\n\r\n  # If The Word Exists \r\n  if word in word_count:\r\n\r\n    # Add 1 To Word Count\r\n    word_count[word] += 1\r\n\r\n  # If Word Is New\r\n  else:\r\n\r\n    # Set Word Count To 1\r\n    word_count[word] = 1\r\n\r\nprint(word_count)\r\n\r\n# Create Variables To Store Results\r\nmost_common_word = None\r\nhighest_count = 0\r\n\r\n# Loop On Dictionary Items\r\nfor word, count in word_count.items():\r\n  if count > highest_count:\r\n    most_common_word = word\r\n    highest_count = count\r\n\r\nprint(\"Most Common Word\", most_common_word)\r\nprint(\"Number Of Repeats\", highest_count)\r\n<\/script>","protected":false},"excerpt":{"rendered":"<p>\u062d\u0633\u0627\u0628 \u0623\u0643\u062b\u0631 \u0643\u0644\u0645\u0629 \u0645\u062a\u0643\u0631\u0631\u0629 \u0641\u064a \u0627\u0644\u0646\u0635 \u0628\u062f\u0648\u0646 \u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0645\u0643\u062a\u0628\u0627\u062a<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[613],"tags":[],"class_list":["post-14897","post","type-post","status-publish","format-standard","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/14897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/comments?post=14897"}],"version-history":[{"count":2,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/14897\/revisions"}],"predecessor-version":[{"id":14899,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/posts\/14897\/revisions\/14899"}],"wp:attachment":[{"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/media?parent=14897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/categories?post=14897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/elzero.org\/old\/wp-json\/wp\/v2\/tags?post=14897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}