{"id":18811,"date":"2022-01-13T15:54:02","date_gmt":"2022-01-13T10:24:02","guid":{"rendered":"https:\/\/java2blog.com\/?p=18811"},"modified":"2023-11-25T09:13:02","modified_gmt":"2023-11-25T03:43:02","slug":"check-if-variable-is-string-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/check-if-variable-is-string-python\/","title":{"rendered":"Check if Variable Is String in Python"},"content":{"rendered":"<p>In this post, we will see what is a string in Python and how to check whether a given variable is a string or not.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#How_to_check_if_a_given_variable_is_of_the_string_type_in_Python\">How to check if a given variable is of the string type in Python?<\/a><ul><li><a href=\"#Using_the_isinstance_function\">Using the isinstance() function.<\/a><\/li><li><a href=\"#Using_the_type_function\">Using the type() function.<\/a><\/li><\/ul><\/li><li><a href=\"#Check_if_function_parameter_is_String\">Check if function parameter is String<\/a><\/li><\/ul><\/div>\n\nThere are many options in Python when it comes to making a choice of selecting a data type and this sometimes creates a need This tutorial focuses on and demonstrates the different ways available to check if a given variable is of the string type in Python.<\/p>\n<h5>What is a string in Python?<\/h5>\n<p>A <code>string<\/code> is one of the widely used data types in Python and it is utilized to represent the Unicode characters in a variable. A string can be declared with either double or single quotes and has great flexibility, meaning it can easily hold any sort of value.<\/p>\n<p>Here, we show a simple example of how to declare a string.<\/p>\n<pre code=\"Python\">\nprint(\"web\") # or print('web')\n<\/pre>\n<h2><span id=\"How_to_check_if_a_given_variable_is_of_the_string_type_in_Python\">How to check if a given variable is of the string type in Python?<\/span><\/h2>\n<ol>\n<li>Using the <code>isinstance()<\/code> function.<\/li>\n<li>Using the <code>type()<\/code> function.<\/li>\n<\/ol>\n<h3><span id=\"Using_the_isinstance_function\">Using the <code>isinstance()<\/code> function.<\/span><\/h3>\n<p>The <code>isinstance()<\/code> function can be utilized for checking whether any given variable is of the desired data type or not.<\/p>\n<p>The <code>isinstance(x, y)<\/code> function takes in two arguments.<\/p>\n<ul>\n<li><strong>x:<\/strong> This is any given variable<\/li>\n<li><strong>y:<\/strong> This here can be any data type that you want to check the variable for. In our case, the value of this will be <code>str<\/code>.<\/li>\n<\/ul>\n<p>The following code uses the <code>isinstance()<\/code> function to check if a given variable is of the string type in Python.<\/p>\n<pre code=\"Python\">\nvar1 = \"football\"\nsol = isinstance(var1, str)\nprint(\"Is it a string? \", sol)\n<\/pre>\n<p>The above code provides the following output:<\/p>\n<div class=\"content-box-green\">\nIs it a string?  True\n<\/div>\n<p>We can simply utilize this function to check if a given variable is of the string type in Python. However, in Python 2, we can use the same function with the argument as a <code>basestring<\/code> class.<\/p>\n<p>A <code>basestring<\/code> class, which is an abstract of both the <code>unicode<\/code> and the <code>str<\/code> class is utilized inside the <code>isinstance()<\/code> function.<\/p>\n<p>Here&#8217;s an example code that shows this abstract class to implement the task:<\/p>\n<pre code=\"Python\">\nvar1 = 'football'\nsol = isinstance(var1, basestring)\nprint(\"Is it a string? \", sol)\n<\/pre>\n<p>The above code provides the following output:<\/p>\n<div class=\"content-box-green\">\n(&#8216;Is it a string? &#8216;, True)\n<\/div>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/python-print-type-of-variable\/\" target=\"_blank\" rel=\"noopener\">Print type of variable in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/python-print-type-of-variable\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/check-if-variable-exists-python\/\" target=\"_blank\" rel=\"noopener\">How to check if variable exists in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/check-if-variable-exists-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<h3><span id=\"Using_the_type_function\">Using the <code>type()<\/code> function.<\/span><\/h3>\n<p>The <code>type()<\/code> function is utilized to simply get the data type of any given variable. The <code>type()<\/code> variable can be utilized with basic <code>==<\/code> operator or even with the <code>is<\/code> operator to check if a given variable is of the string type in Python.<\/p>\n<p>The following code uses the <code>type()<\/code> function to check if a given variable is of the string type in Python.<\/p>\n<pre code=\"Python\">\nvar1 = \"football\"\nsol = type(var1) == str\nprint(\"Is it a string? \", sol)\n<\/pre>\n<p>The above code provides the following output:<\/p>\n<div class=\"content-box-green\">\nIs it a string?  True\n<\/div>\n<h2><span id=\"Check_if_function_parameter_is_String\">Check if function parameter is String<\/span><\/h2>\n<p>We can use above methods to test if function parameter is String or not.<\/p>\n<p>Let&#8217;s write a function to find length of String if datatype is String. If it is not String, then ignore it.<\/p>\n<pre code=\"python\">\nstr1 = 789\n\nstr2 = 'Java2blog'\n\ndef length_of_string(strInput):\n    # We can also use \"if isinstance(inp, str)\"\n    if  not isinstance(strInput, str):\n        print('Input is not a String')\n    else:\n        print(len(strInput))\n\nlength_of_string(str1)\nlength_of_string(str2)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\nInput is not a String<br \/>\n9\n<\/div>\n<p>You can also use type() function rather than isInstance here.<\/p>\n<p>That&#8217;s all about how to check if variable is String in Python<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":18827,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[145,186],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18811"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=18811"}],"version-history":[{"count":3,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18811\/revisions"}],"predecessor-version":[{"id":25695,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/18811\/revisions\/25695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media\/18827"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=18811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=18811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=18811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}