{"id":8141,"date":"2021-06-07T18:23:17","date_gmt":"2021-06-07T12:53:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=8141"},"modified":"2021-11-22T18:38:37","modified_gmt":"2021-11-22T13:08:37","slug":"python-program-to-check-if-a-string-is-a-pangram-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-if-a-string-is-a-pangram-or-not\/","title":{"rendered":"Python Program to Check if a String is a Pangram or Not"},"content":{"rendered":"
Strings in Python:<\/strong><\/p>\n A Python string is an ordered collection of characters used to express and store text-based data. Strings are saved in an adjacent memory area as individual characters. It is accessible in both directions: forward and backward. Characters are merely symbols. Strings are immutable Data Types in Python, which means they cannot be modified once they are formed.<\/p>\n Pangram:<\/strong><\/p>\n If a sentence or string contains all 26 letters of the English alphabet at least once, it is considered to be a pangram.<\/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: There are several ways to check if the given string is pangram or not some of them are:<\/p>\n Drive into Python Programming Examples<\/a> and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The following program can be easily implemented in Python by using the set() method.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n This method is only applicable if the given string contains alphabets.<\/p>\n The following program can be easily implemented in Python by using the counter() function<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Note:<\/strong><\/p>\n This method is only applicable if the given string contains alphabets.<\/p>\n Related Programs<\/strong>:<\/p>\n Strings in Python: A Python string is an ordered collection of characters used to express and store text-based data. Strings are saved in an adjacent memory area as individual characters. It is accessible in both directions: forward and backward. Characters are merely symbols. Strings are immutable Data Types in Python, which means they cannot be …<\/p>\ngiven string =\"Helloabcdfegjilknmporqstvuxwzy\"<\/pre>\n
The given string helloabcdfegjilknmporqstvuxwzy is a pangram<\/pre>\n
given string =\"hellothisisbtechgeeks\"<\/pre>\n
\n<\/strong><\/p>\nThe given string hellothisisbtechgeeks is not a pangram<\/pre>\n
Python Program to Check if a String is a Pangram or Not<\/h2>\n
\n
Method #1:Naive Approach<\/h3>\n
\n
# Python Program to Check if a String is a Pangram or Not\r\ndef checkPangramString(string):\r\n\r\n # creating a new string (alphabet string which stores all the alphabets of the english language\r\n AlphabetString = 'abcdefghijklmnopqrstuvwxyz'\r\n\r\n # Traverse through the alphabets string\r\n for char in AlphabetString:\r\n # Check if this character is present in given string .\r\n if char not in string.lower():\r\n # if yes then this character is not available hence return False\r\n return False\r\n # After the end of loop return True (which implies it is pangram)\r\n return True\r\n\r\n\r\n# given string\r\nstring = \"Helloabcdfegjilknmporqstvuxwzy\"\r\n# converting the given string into lower case\r\nstring = string.lower()\r\n# passing this string to checkPangramString function which returns true\r\n# if the given string is pangram else it will return false\r\n\r\nif checkPangramString(string):\r\n print(\"The given string\", string, \"is a pangram\")\r\nelse:\r\n print(\"The given string\", string, \"is not a pangram\")\r\n<\/pre>\n
The given string helloabcdfegjilknmporqstvuxwzy is a pangram<\/pre>\n
Method #2:Using set() method<\/h3>\n
\n
# Python Program to Check if a String is a Pangram or Not\r\ndef checkPangramString(string):\r\n\r\n # converting given string to set using set() function\r\n setString = set(string)\r\n # calculate the length of the set\r\n length = len(setString)\r\n # If the length is 26, it is a pangram so return true\r\n if(length == 26):\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n# given string\r\nstring = \"Helloabcdfegjilknmporqstvuxwzy\"\r\n# converting the given string into lower case\r\nstring = string.lower()\r\n# passing this string to checkPangramString function which returns true\r\n# if the given string is pangram else it will return false\r\n\r\nif checkPangramString(string):\r\n print(\"The given string\", string, \"is a pangram\")\r\nelse:\r\n print(\"The given string\", string, \"is not a pangram\")\r\n<\/pre>\n
The given string helloabcdfegjilknmporqstvuxwzy is a pangram<\/pre>\n
Method #3:Using Counter() function (Hashing)<\/h3>\n
\n
# Python Program to Check if a String is a Pangram or Not\r\n# importing counter from collections\r\nfrom collections import Counter\r\n\r\n\r\ndef checkPangramString(string):\r\n\r\n # Calculate the frequency of all characters in the given string\r\n # using Counter() method\r\n frequ = Counter(string)\r\n # calculate the length of the frequency dictionary which is\r\n # returned from counter() function\r\n length = len(frequ)\r\n # If the length is 26, it is a pangram so return true\r\n if(length == 26):\r\n return True\r\n else:\r\n return False\r\n\r\n\r\n# given string\r\nstring = \"Helloabcdfegjilknmporqstvuxwzy\"\r\n# converting the given string into lower case\r\nstring = string.lower()\r\n# passing this string to checkPangramString function which returns true\r\n# if the given string is pangram else it will return false\r\n\r\nif checkPangramString(string):\r\n print(\"The given string\", string, \"is a pangram\")\r\nelse:\r\n print(\"The given string\", string, \"is not a pangram\")\r\n<\/pre>\n
The given string helloabcdfegjilknmporqstvuxwzy is a pangram<\/pre>\n
\n