{"id":17048,"date":"2021-09-09T12:37:20","date_gmt":"2021-09-09T07:07:20","guid":{"rendered":"https:\/\/tutorial.eyehunts.com\/?p=17048"},"modified":"2022-04-27T18:18:15","modified_gmt":"2022-04-27T12:48:15","slug":"python-check-if-variable-is-string-example-code","status":"publish","type":"post","link":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/","title":{"rendered":"Python check if variable is string | Example code"},"content":{"rendered":"\n<p>Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the second is the type you want to check for.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>isinstance(Var, Data_Type)<\/code><\/pre>\n\n\n\n<div style=\"height:30px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Python checks if the variable is a string example<\/strong><\/h3>\n\n\n\n<p>Simple example code, The first parameter will be given a variable, and the second is the <a href=\"https:\/\/tutorial.eyehunts.com\/js\/javascript-typeof-operator-find-the-data-type\/\">data type <\/a>string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var = \"String\"\n\nprint(isinstance(var, str))\n\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=\"248\" height=\"122\" src=\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-string.jpg?resize=248%2C122&#038;ssl=1\" alt=\"Python check if variable is string\" class=\"wp-image-19425\"\/><\/figure><\/div>\n\n\n\n<p><strong>Another Example<\/strong><\/p>\n\n\n\n<p>Uses the <a href=\"https:\/\/tutorial.eyehunts.com\/python\/python-type-function-get-object-type\/\">type <\/a>built-in function to see if the type of your variable is str.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var = \"String\"\n\nif type(var) == str:\n    print(\"String type\")\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong>: String type<\/p>\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 questions or suggestions on this Python var 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>Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the second is the type you want to check for. Python checks if the variable is a string example Simple example code, The first parameter will be given a variable, and&hellip;&nbsp;<a href=\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Python check if variable is 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":[181,98,31,32],"post_series":[],"class_list":["post-17048","post","type-post","status-publish","format-standard","hentry","category-python","tag-python-if","tag-python-string","tag-python-variable-types","tag-python-variables"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.1.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python check if variable is string | Example code<\/title>\n<meta name=\"description\" content=\"Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the..\" \/>\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-if-variable-is-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 if variable is string | Example code\" \/>\n<meta property=\"og:description\" content=\"Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the..\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-09T07:07:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-27T12:48:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-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-if-variable-is-string-example-code\/\",\"url\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/\",\"name\":\"Python check if variable is string | Example code\",\"isPartOf\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-string.jpg\",\"datePublished\":\"2021-09-09T07:07:20+00:00\",\"dateModified\":\"2022-04-27T12:48:15+00:00\",\"author\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1\"},\"description\":\"Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the..\",\"breadcrumb\":{\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#primaryimage\",\"url\":\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-string.jpg?fit=248%2C122&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-string.jpg?fit=248%2C122&ssl=1\",\"width\":248,\"height\":122},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tutorial.eyehunts.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python check if variable is 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 if variable is string | Example code","description":"Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the..","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-if-variable-is-string-example-code\/","og_locale":"en_US","og_type":"article","og_title":"Python check if variable is string | Example code","og_description":"Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the..","og_url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/","og_site_name":"Tutorial","article_published_time":"2021-09-09T07:07:20+00:00","article_modified_time":"2022-04-27T12:48:15+00:00","og_image":[{"url":"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-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-if-variable-is-string-example-code\/","url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/","name":"Python check if variable is string | Example code","isPartOf":{"@id":"https:\/\/tutorial.eyehunts.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#primaryimage"},"image":{"@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#primaryimage"},"thumbnailUrl":"https:\/\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-string.jpg","datePublished":"2021-09-09T07:07:20+00:00","dateModified":"2022-04-27T12:48:15+00:00","author":{"@id":"https:\/\/tutorial.eyehunts.com\/#\/schema\/person\/69ca2cb8c13fdce0ee5b39d6175119b1"},"description":"Use the isinstance built-in function to check if the variable is a string in Python. It takes two arguments, the first is variable, and the..","breadcrumb":{"@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#primaryimage","url":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-string.jpg?fit=248%2C122&ssl=1","contentUrl":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/09\/Python-check-if-variable-is-string.jpg?fit=248%2C122&ssl=1","width":248,"height":122},{"@type":"BreadcrumbList","@id":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-variable-is-string-example-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tutorial.eyehunts.com\/"},{"@type":"ListItem","position":2,"name":"Python check if variable is 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":17054,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-if-variable-is-string-example-code\/","url_meta":{"origin":17048,"position":0},"title":"Python if variable is string | Example code","author":"Rohit","date":"July 25, 2021","format":false,"excerpt":"There are two ways to check if a variable is a string in Python. First isinstance() method and second type() method. How to check if a variable is a string in Python Simple python examples code: Using isinstance(var, str) The isinstance function takes two arguments. The first is your variable.\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python if variable is string","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/07\/Python-if-variable-is-string.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":16950,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-if-not-string-example-code\/","url_meta":{"origin":17048,"position":1},"title":"Python if not String | Example code","author":"Rohit","date":"November 2, 2021","format":false,"excerpt":"There are 2 ways to check is NOT a string in python. First, use the isinstance() function, and uses the type function with an if statement. Python if not String example Simple example code. Using isInstance() By giving the second argument as \u201cstr\u201d Check if Not string, This method can\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python if not String","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/11\/Python-if-not-String.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":17056,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-for-string-example-code\/","url_meta":{"origin":17048,"position":2},"title":"Python check for string | Example code","author":"Rohit","date":"July 29, 2021","format":false,"excerpt":"Use the isinstance() method to check for a string in Python. This method can be used to test whether any variable is a particular (string) datatype or not. If yes then return True. isinstance(give_string, str) Python check for string Example code Python code to demonstrate check if the variable is\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python check for string","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/07\/Python-check-for-string.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":17063,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-check-if-the-object-is-string-example-code\/","url_meta":{"origin":17048,"position":3},"title":"Python check if the object is string | example code","author":"Rohit","date":"July 28, 2021","format":false,"excerpt":"The best way to checks if the object is a string is by using the isinstance() method in Python. This function returns True if the given object is an instance of class classified or any subclass of class info, otherwise returns False. isinstance(object, class) How to find out if a\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python check if the object is string","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/07\/Python-check-if-the-object-is-string.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":17098,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-print-type-of-variable-example-code\/","url_meta":{"origin":17048,"position":4},"title":"Python print type of variable | Example code","author":"Rohit","date":"July 25, 2021","format":false,"excerpt":"Use the Python type() built-in function to check the variable type and after that show the result in the console with the help of the print method. type(object) If want to check if a variable is of a given type, use the isinstance method. isinstance(object, classtype) How to determine a\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-print-type-of-variable.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":17065,"url":"https:\/\/tutorial.eyehunts.com\/python\/python-instanceof-string-example-code\/","url_meta":{"origin":17048,"position":5},"title":"Python instanceof string | Example code","author":"Rohit","date":"November 5, 2021","format":false,"excerpt":"Use the isinstance() method to check whether the given variable is an instanceof string or not in Python. Syntax if the type is a string isinstance(s, str) Python instanceof string example Simple example code is instance string or not in Python. x = 100 s = \"Hello\" print(isinstance(x, str)) print(isinstance(s,\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/tutorial.eyehunts.com\/category\/python\/"},"img":{"alt_text":"Python instanceof string","src":"https:\/\/i0.wp.com\/tutorial.eyehunts.com\/wp-content\/uploads\/2021\/11\/Python-instanceof-string.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\/17048","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=17048"}],"version-history":[{"count":1,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/17048\/revisions"}],"predecessor-version":[{"id":28837,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/posts\/17048\/revisions\/28837"}],"wp:attachment":[{"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/media?parent=17048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/categories?post=17048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/tags?post=17048"},{"taxonomy":"post_series","embeddable":true,"href":"https:\/\/tutorial.eyehunts.com\/wp-json\/wp\/v2\/post_series?post=17048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}