{"id":15115,"date":"2021-10-01T11:00:34","date_gmt":"2021-10-01T05:30:34","guid":{"rendered":"https:\/\/python-programs.com\/?p=15115"},"modified":"2021-11-22T18:33:26","modified_gmt":"2021-11-22T13:03:26","slug":"python-program-to-count-palindrome-words-in-a-sentence","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-palindrome-words-in-a-sentence\/","title":{"rendered":"Python Program to Count Palindrome Words in a Sentence"},"content":{"rendered":"
Don’t miss the chance of Java programs examples with output pdf free download<\/a> as it is very essential for all beginners to experienced programmers for cracking the interviews.<\/p>\n Given a sentence\/string the task is to count the number of palindromic words in the given string in Python.<\/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 Below are the ways to Count Palindrome words in the given sentence in Python<\/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 Related Programs<\/strong>:<\/p>\n Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews. Given a sentence\/string the task is to count the number of palindromic words in the given string in Python. Examples: Example1: Input: Given Sentence =madam how are …<\/p>\nGiven Sentence =madam how are you<\/pre>\n
The total number of palindromic words in the given sentence { madam how are you } are 1<\/pre>\n
Given Sentence = helleh this issi btechgeeksskeeghcetb pyyp<\/pre>\n
The total number of palindromic words in the given sentence { helleh this issi btechgeeksskeeghcetb pyyp } are 4<\/pre>\n
Program to Count Palindrome Words in a Sentence in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the sentence\/string as static input and store it in a variable.\r\ngvnsentence =\"helleh this issi btechgeeksskeeghcetb pyyp\"\r\n# Take a variable palcnt which stores the count of palindromic words\r\n# in the given sentence and initialize its value to 0.\r\npalcnt = 0\r\n# Split the given sentence into a list of words using\r\n# the built-in function split() and store it in a variable.\r\nsentcewords = gvnsentence.split()\r\n# Traverse in this list of words using the For loop.\r\nfor itrword in sentcewords:\r\n # Inside the for loop,Reverse the iterator word\r\n # using slicing and store it in a variable.\r\n reveword = itrword[::-1]\r\n # Check if this reverse word is equal to the\r\n # iterator word(Palindrome Condition) using the If conditional statement.\r\n if(reveword == itrword):\r\n # If it is true then increment the value of palcnt by 1.\r\n palcnt = palcnt+1\r\n\r\n\r\n# Print the palcnt value.\r\nprint(\r\n 'The total number of palindromic words in the given sentence {', gvnsentence, '} are ', palcnt)\r\n<\/pre>\n
The total number of palindromic words in the given sentence { helleh this issi btechgeeksskeeghcetb pyyp } are 4<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the sentence\/string as user input using the input() function and store it in a variable.\r\ngvnsentence = input('Enter some random sentence =')\r\n# Take a variable palcnt which stores the count of palindromic words\r\n# in the given sentence and initialize its value to 0.\r\npalcnt = 0\r\n# Split the given sentence into a list of words using\r\n# the built-in function split() and store it in a variable.\r\nsentcewords = gvnsentence.split()\r\n# Traverse in this list of words using the For loop.\r\nfor itrword in sentcewords:\r\n # Inside the for loop,Reverse the iterator word\r\n # using slicing and store it in a variable.\r\n reveword = itrword[::-1]\r\n # Check if this reverse word is equal to the\r\n # iterator word(Palindrome Condition) using the If conditional statement.\r\n if(reveword == itrword):\r\n # If it is true then increment the value of palcnt by 1.\r\n palcnt = palcnt+1\r\n\r\n\r\n# Print the palcnt value.\r\nprint(\r\n 'The total number of palindromic words in the given sentence {', gvnsentence, '} are ', palcnt)\r\n<\/pre>\n
Enter some random sentence =madam how are you\r\nThe total number of palindromic words in the given sentence { madam how are you } are 1<\/pre>\n
\n