{"id":3612,"date":"2020-02-26T11:39:49","date_gmt":"2020-02-26T11:39:49","guid":{"rendered":"https:\/\/www.askpython.com\/?p=3612"},"modified":"2023-02-16T19:57:14","modified_gmt":"2023-02-16T19:57:14","slug":"python-string-isspace","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/string\/python-string-isspace","title":{"rendered":"Python String isspace() method"},"content":{"rendered":"\n<p>In this article, we will dive into the working of <strong>Python String isspace() method<\/strong>.<strong><a href=\"https:\/\/www.askpython.com\/python\/string\/python-string-functions\" class=\"rank-math-link\">Python String<\/a><\/strong> has got vivid built-in methods to work with string input.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started with Python String isspace()<\/h2>\n\n\n\n<p>The Python string <code>isspace()<\/code> method is used to check the presence of <strong>white spaces<\/strong> within the input string. <\/p>\n\n\n\n<p><strong>The white space characters include<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\\n<\/li><li>\\t<\/li><li>\\v<\/li><li>\\f<\/li><li>&#8216; &#8216;<\/li><li>\\r <\/li><li>etc.<\/li><\/ul>\n\n\n\n<p>It returns <strong>True<\/strong> if the input string contains <strong>only<\/strong> white spaces. Otherwise, the function returns <strong>False<\/strong> when the string contains one or more non-white-space characters.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninput_string.isspace()\n<\/pre><\/div>\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp = &#039;Engineering_Discipline&#039;\n\nprint(inp.isspace()) \n<\/pre><\/div>\n\n\n<p>In the above example, as the input string contains no white space, the function returns False.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nFalse\n<\/pre><\/div>\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp = &#039;\\t \\v \\n&#039;\n  \nprint(inp.isspace()) \n  \n<\/pre><\/div>\n\n\n<p>In this example, the input string consists only of white spaces. Thus, the function evaluates to True.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nTrue\n<\/pre><\/div>\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ninp = &#039;\\thello everyone!!\\n&#039;\n  \nprint(inp.isspace()) \n  \n<\/pre><\/div>\n\n\n<p>In this example, the input string contains a combination of white space characters and non-white-space characters i.e. it contains one or more non-white-space characters.<\/p>\n\n\n\n<p>Thus, the function returns False.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nFalse\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">NumPy isspace() method<\/h2>\n\n\n\n<p>Python <strong><a class=\"rank-math-link rank-math-link\" href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\">NumPy module<\/a><\/strong> provides us with the <strong>numpy.char.isspace()<\/strong> <strong>method <\/strong>to check the presence of white spaces in the input elements of the array.<\/p>\n\n\n\n<p><strong>Syntax<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.char.isspace(input_array)\n<\/pre><\/div>\n\n\n<p>The <code>numpy.char.isspace()<\/code> function checks for the presence of white spaces in the input array in an element-wise fashion. <\/p>\n\n\n\n<p>That is, it checks it for every element of the array and returns true or false for each and every element present.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n  \n\ninp_arr1 = numpy.array(&#x5B; &#039;Science&#039;, &#039;Commerce&#039;, &#039;Arts&#039;] ) \nprint (&quot;Elements of array1:\\n&quot;, inp_arr1)  \n  \nres1 = numpy.char.isspace(inp_arr1) \nprint (&quot;Array1 after using isspace():\\n&quot;, res1) \n\ninp_arr2 = numpy.array(&#x5B; &#039;Sci\\nence&#039;, &#039;Commerce\\t&#039;, &#039;Arts&#039;] ) \nprint (&quot;Elements of array2:\\n&quot;, inp_arr2)  \n  \nres2 = numpy.char.isspace(inp_arr2) \nprint (&quot;Array2 after using isspace():\\n&quot;, res2) \n\ninp_arr3 = numpy.array(&#x5B; &#039;\\n\\r&#039;, &#039;\\f\\t&#039;, &#039; &#039;] ) \nprint (&quot;Elements of array3:\\n&quot;, inp_arr3)  \n  \nres3 = numpy.char.isspace(inp_arr3) \nprint (&quot;Array3 after using isspace():\\n&quot;, res3) \n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nElements of array1:\n &#x5B;&#039;Science&#039; &#039;Commerce&#039; &#039;Arts&#039;]\nArray1 after using isspace():\n &#x5B;False False False]\n\nElements of array2:\n &#x5B;&#039;Sci\\nence&#039; &#039;Commerce\\t&#039; &#039;Arts&#039;]\nArray2 after using isspace():\n &#x5B;False False False]\n\nElements of array3:\n &#x5B;&#039;\\n\\r&#039; &#039;\\x0c\\t&#039; &#039; &#039;]\nArray3 after using isspace():\n &#x5B; True  True  True]\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Pandas isspace() method<\/h2>\n\n\n\n<p><strong><a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" class=\"rank-math-link\">Pandas module<\/a><\/strong> includes isspace() function to check the white-space character strings in the entire data present in the Series or DataFrame.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nSeries.str.isspace()\n<\/pre><\/div>\n\n\n<p>The <code>Series.str.isspace()<\/code> method checks for the presence of the white space strings for every element and return <strong>True <\/strong>only for those elements. <\/p>\n\n\n\n<p><strong>Note<\/strong>: Series.str.isspace() method works only for string type elements. If the interpreter encounters any non-string value, it raises a ValueError Exception.<\/p>\n\n\n\n<p><strong>The above exception can be controlled using <code>.astype()<\/code> function. The .astype() function converts the non-string type data to string type.<\/strong><\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n \nimport pandas \n\ninp_data = pandas.Series(&#x5B;&#039;Jim&#039;, &#039;Jonny&#039;, &#039; &#039;, &#039;\\t&#039;, &#039;Daisy&#039;, &#039;\\n&#039;]) \n\nres = inp_data.str.isspace() \n\nprint(res) \n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n0    False\n1    False\n2     True\n3     True\n4    False\n5     True\ndtype: bool\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we have understood the functioning of the Python string isspace() method.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p>Python isspace() method<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will dive into the working of Python String isspace() method.Python String has got vivid built-in methods to work with string input. Getting started with Python String isspace() The Python string isspace() method is used to check the presence of white spaces within the input string. The white space characters include: \\n [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":3630,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-string"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=3612"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/3612\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/3630"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=3612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=3612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=3612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}