{"id":16506,"date":"2021-09-06T21:48:42","date_gmt":"2021-09-06T16:18:42","guid":{"rendered":"https:\/\/tutorial.eyehunts.com\/?p=16506"},"modified":"2022-04-07T18:38:11","modified_gmt":"2022-04-07T13:08:11","slug":"python-check-empty-string-example-code","status":"publish","type":"post","link":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/","title":{"rendered":"Python check empty string | Example code"},"content":{"rendered":"\n<p>The most elegant way would probably be to simply check an empty string if it&#8217;s true or falsy in Python. <\/p>\n\n\n\n<p>Use<a href=\"https:\/\/tutorial.eyehunts.com\/js\/javascript-if-statement-basic-code\/\"> if statement<\/a> <strong>evaluated<\/strong> to <code>False<\/code> or <code>True<\/code> in a Boolean context if they are empty or not. They are <strong>not equal<\/strong> to <code>False<\/code> or <code>True<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if not myString:<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>For example check an empty string in Python<\/strong><\/h2>\n\n\n\n<p>Empty strings are &#8220;falsy&#8221; (<a href=\"http:\/\/docs.python.org\/2\/library\/stdtypes.html#truth-value-testing\">python 2<\/a> or <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html#truth-value-testing\">python 3<\/a> references), which means they are considered false in a <a href=\"https:\/\/tutorial.eyehunts.com\/python\/true-false-python-booleans-represent\/\">Boolean <\/a>context, so you can just do this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_string = \"\"\n\nif not my_string:\n    print(\"The string is empty\")\nelse:\n    print(\"The string is not empty\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"276\" height=\"112\" src=\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg?resize=276%2C112&#038;ssl=1\" alt=\"Python check empty string\" class=\"wp-image-19265\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Do comment if you have any doubts or suggestions on this Python string topic.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note:<\/strong> IDE:&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.jetbrains.com\/pycharm\/\" target=\"_blank\">PyCharm<\/a>&nbsp;2021.3.3 (Community Edition)<\/p><p>Windows 10<\/p><p><strong>Python 3.10.1<\/strong><\/p><p>All<strong>&nbsp;Python Examples&nbsp;are in&nbsp;Python&nbsp;3<\/strong>, so Maybe its different from python 2 or upgraded versions.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>The most elegant way would probably be to simply check an empty string if it&#8217;s true or falsy in Python. Use if statement evaluated to False or True in a Boolean context if they are empty or not. They are not equal to False or True. For example check an empty string in Python Empty&hellip;&nbsp;<a href=\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Python check empty string | Example code<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[30],"tags":[177,98],"post_series":[],"class_list":["post-16506","post","type-post","status-publish","format-standard","hentry","category-python","tag-python-empty","tag-python-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python check empty string | Example code - EyeHunts<\/title>\n<meta name=\"description\" content=\"The most elegant way would probably be to simply check empty string if it&#039;s true or falsy in Python. Example check an empty string in Python\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python check empty string | Example code - EyeHunts\" \/>\n<meta property=\"og:description\" content=\"The most elegant way would probably be to simply check empty string if it&#039;s true or falsy in Python. Example check an empty string in Python\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-06T16:18:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-07T13:08:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg\" \/>\n<meta name=\"author\" content=\"Rohit\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rohit\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/\",\"url\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/\",\"name\":\"Python check empty string | Example code - EyeHunts\",\"isPartOf\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg\",\"datePublished\":\"2021-09-06T16:18:42+00:00\",\"dateModified\":\"2022-04-07T13:08:11+00:00\",\"author\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1\"},\"description\":\"The most elegant way would probably be to simply check empty string if it's true or falsy in Python. Example check an empty string in Python\",\"breadcrumb\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg?fit=276%2C112&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg?fit=276%2C112&ssl=1\",\"width\":276,\"height\":112},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tutorial.eyehunts.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python check empty string | Example code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/#website\",\"url\":\"https:\/\/tutorial.eyehunts.com\/\",\"name\":\"Tutorial\",\"description\":\"By EyeHunts\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tutorial.eyehunts.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1\",\"name\":\"Rohit\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971\",\"contentUrl\":\"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971\",\"caption\":\"Rohit\"},\"description\":\"Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology &amp; like learning technical.\",\"url\":\"https:\/\/tutorial.eyehunts.com\/author\/rohit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python check empty string | Example code - EyeHunts","description":"The most elegant way would probably be to simply check empty string if it's true or falsy in Python. Example check an empty string in Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/","og_locale":"en_US","og_type":"article","og_title":"Python check empty string | Example code - EyeHunts","og_description":"The most elegant way would probably be to simply check empty string if it's true or falsy in Python. Example check an empty string in Python","og_url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/","og_site_name":"Tutorial","article_published_time":"2021-09-06T16:18:42+00:00","article_modified_time":"2022-04-07T13:08:11+00:00","og_image":[{"url":"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg","type":"","width":"","height":""}],"author":"Rohit","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rohit","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/","url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/","name":"Python check empty string | Example code - EyeHunts","isPartOf":{"@id":"https:\/\/tutorial.eyehunts.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#primaryimage"},"image":{"@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#primaryimage"},"thumbnailUrl":"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg","datePublished":"2021-09-06T16:18:42+00:00","dateModified":"2022-04-07T13:08:11+00:00","author":{"@id":"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1"},"description":"The most elegant way would probably be to simply check empty string if it's true or falsy in Python. Example check an empty string in Python","breadcrumb":{"@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#primaryimage","url":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg?fit=276%2C112&ssl=1","contentUrl":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-empty-string.jpg?fit=276%2C112&ssl=1","width":276,"height":112},{"@type":"BreadcrumbList","@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-empty-string-example-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tutorial.eyehunts.com\/"},{"@type":"ListItem","position":2,"name":"Python check empty string | Example code"}]},{"@type":"WebSite","@id":"https:\/\/tutorial.eyehunts.com\/#website","url":"https:\/\/tutorial.eyehunts.com\/","name":"Tutorial","description":"By EyeHunts","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tutorial.eyehunts.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1","name":"Rohit","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/image\/","url":"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971","contentUrl":"https:\/\/tutorial.eyehunts.com\/wp-content\/litespeed\/avatar\/2b27529b86d6dfb5336897e07c93a827.jpg?ver=1777374971","caption":"Rohit"},"description":"Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology &amp; like learning technical.","url":"https:\/\/tutorial.eyehunts.com\/author\/rohit\/"}]}},"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":16512,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-test-empty-string-example-code\/","url_meta":{"origin":16506,"position":0},"title":"Python test empty string | Example code","author":"Rohit","date":"July 14, 2021","format":false,"excerpt":"Python Empty strings are \"falsy\" (python 2 or python 3 references), which means they are considered false in a Boolean context, so you have to just do this: if not myString: Example code test empty string in python Simple python example code. The most elegant way would probably be to\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/07\/Python-test-empty-string.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":16502,"url":"https:\/\/tutorial.eyehunts.com\/python\/what-is-python-empty-string-example-code\/","url_meta":{"origin":16506,"position":1},"title":"What is Python empty string | Example code","author":"Rohit","date":"July 21, 2021","format":false,"excerpt":"Python string is called an empty string if the string doesn't have any character, whitespace whatsoever. But if the string has multiple white spaces or single space or tab spaces then it is a blank string. Example Create and check an empty string in Python. Simple python example code. In\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python empty string","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/07\/Python-empty-string.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":8170,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-string-is-empty-or-whitespace\/","url_meta":{"origin":16506,"position":2},"title":"Python check if string is empty or whitespace | Example code","author":"Rohit","date":"April 23, 2020","format":false,"excerpt":"You can check if the string is empty or whitespace using an isspace() function and strip() function in python. You can also just use not string with if-else condition statement. Check if the string is \"whitespace\" in Python First, check if the string\u00a0contains\u00a0whitespace only or not by using a \"isspace()\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":16504,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-string-is-empty-4-ways-example-code\/","url_meta":{"origin":16506,"position":3},"title":"Python check if string is empty | 4 ways Example code","author":"Rohit","date":"July 18, 2021","format":false,"excerpt":"There are several ways to checks if the string is empty or not in python but the most elegant way would probably be to simply check if it's true or falsy. if not my_string: How to Check If String is Empty or Not in Python example Check python examples in\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python check if string is empty","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/07\/Python-check-if-string-is-empty.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":17615,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-remove-empty-elements-from-list-example-code\/","url_meta":{"origin":16506,"position":4},"title":"Python remove empty elements from list | Example code","author":"Rohit","date":"August 6, 2021","format":false,"excerpt":"The easiest way is list comprehension to remove empty elements from a list in Python. And another way is to use the filter() method. The empty string \"\" contains no characters and empty elements could be None or [ ], etc. Python remove empty elements from a list Example Simple\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python remove empty elements from list","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/08\/Python-remove-empty-elements-from-list.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":36049,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-any-all-functions\/","url_meta":{"origin":16506,"position":5},"title":"Python any all | functions","author":"Rohit","date":"July 18, 2023","format":false,"excerpt":"In Python, the any() and all() functions are built-in functions that operate on iterables such as lists, tuples, or sets. They provide a concise way to check the truthiness of multiple elements within an iterable. Here's a breakdown of each function: any(iterable): The any() function returns True if at least\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2023\/07\/Python-any-all-functions.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/16506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/comments?post=16506"}],"version-history":[{"count":1,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/16506\/revisions"}],"predecessor-version":[{"id":28113,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/16506\/revisions\/28113"}],"wp:attachment":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/media?parent=16506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/categories?post=16506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/tags?post=16506"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/post_series?post=16506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}