{"id":3430,"date":"2021-04-23T13:25:32","date_gmt":"2021-04-23T07:55:32","guid":{"rendered":"https:\/\/python-programs.com\/?p=3430"},"modified":"2021-11-22T18:44:54","modified_gmt":"2021-11-22T13:14:54","slug":"python-check-if-string-is-empty-or-contain-spaces-only","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-check-if-string-is-empty-or-contain-spaces-only\/","title":{"rendered":"Python: Check if String is Empty or Contain Spaces only"},"content":{"rendered":"
Python strings are byte arrays representing characters of Unicode.. However, because Python lacks a character data type, a single character is simply a one-length string. Square brackets can be used to access string elements.<\/p>\n
Given a string, the task is to check whether the string is empty or not.<\/p>\n
Example:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to check whether the string is empty some of them are:<\/p>\n The most general method for checking for zero-length strings is to use len(). Despite the fact that it ignores the fact that a string with only spaces should be considered an empty string even if it is not zero.<\/p>\n In Python, the len() function takes a sequence as an argument and returns the number of elements in that sequence. So, if we pass a string to the len() function as an argument, it returns the total number of characters in that string.<\/p>\n So, we can use the len() function to determine whether a string is empty or not by determining whether the number of characters in the string is zero or not.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The not operator can do the same thing as len() and search for 0 length strings, but it also considers strings with only spaces to be non-empty, which isn’t realistic.<\/p>\n In Python, an empty string is equivalent to False. So, to see if a string is empty or not, we can use the \u201cnot\u201d operator, .<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n To get a copy of a string without leading and trailing white spaces, we can use the string’s strip() function. So, let’s use this to see if the string is empty or only contains white spaces.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The string class’s isspace() function returns True if the string only contains white spaces. So we can use this to see if a string is empty or only contains white spaces.<\/p>\n To get a copy of a string without leading and trailing white spaces, we can use the string’s strip() function. So, let’s use this to see if the string is empty or only contains white spaces.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Python strings are byte arrays representing characters of Unicode.. However, because Python lacks a character data type, a single character is simply a one-length string. Square brackets can be used to access string elements. Given a string, the task is to check whether the string is empty or not. Example: Input: string=”” Output: Given String …<\/p>\nstring=\"\"<\/pre>\n
Given String is empty<\/pre>\n
Check if String is Empty<\/h2>\n
\n
Method #1:Using len() function<\/h3>\n
# given string\r\nstring = \"\"\r\n# determining whether or not the given string is empty using len()\r\nif len(string) == 0:\r\n print(\"Givem String is empty\")\r\nelse:\r\n print(\"Given String is not empty\")\r\n<\/pre>\n
Given String is empty<\/pre>\n
Method #2:Using not<\/h3>\n
# given string\r\nstring = \"\"\r\n# determining whether or not the given string is empty\r\nif not string:\r\n print(\"Given String is empty\")\r\nelse:\r\n print(\"Given String is not empty\")\r\n<\/pre>\n
Given String is empty<\/pre>\n
Check whether string is empty\/contain spaces<\/h3>\n
Using strip()<\/h3>\n
# given string\r\nstring = \" \"\r\n# determining whether or not the given string is empty\r\nif string and string.strip():\r\nprint(\"Given String is not empty\")\r\nelse:\r\nprint(\"Given String is empty\")\r\n<\/pre>\n
Given String is empty<\/pre>\n
Using isspace()<\/h3>\n
# given string\r\nstring = \" \"\r\n# determining whether or not the given string is empty\r\nif string and not string.isspace():\r\n print(\"Given String is not empty\")\r\nelse:\r\n print(\"Given String is empty\")\r\n<\/pre>\n
Given String is empty<\/pre>\n
\n