In this article we will discuss three different ways to check if the type of a variable is string or not in python.
Check if type of a variable is string in python using isinstance() function
Python provides a function to check the type of a variable i.e.
isinstance(object, classinfo)
This function returns True if the given object is an instance of class classinfo or any sub class of classinfo, otherwise return False
Let’s use this to check if the given variable is of string type or not,
Frequently Asked:
Example 1:
sample_text = "Hello" # Check if the type of a variable is string if isinstance(sample_text, str): print('Type of variable is string') else: print('Type is variable is not string')
Output:
Type of variable is string
Here we passed the variable sample_text as first argument and str (String class) as second argument. As variable sample_text actually contains the string, therefore it isinstance() returned True. It confirms that the type of variable sample_text is string.
Now let’s checkout a negative example,
Latest Python - Video Tutorial
Example 2:
sample_num = 11 # Check if the type of a variable is string if isinstance(sample_num, str): print('Type of variable is string') else: print('Type is variable is not string')
Output:
Type is variable is not string
Here we passed the variable sample_num as first argument and str (String class) as second argument. As variable sample_num actually contains a number, therefore isinstance() returned False in this case. It confirms that the type of variable sample_text is not string.
Check if type of a variable is string in python using type() function
In python, we can use the type(obj) function to get the type of given object. Let’s use this to check if the given variable is of string type or not,
Example 1:
sample_text = "Hello" if type(sample_text) == str: print('Type of variable is string') else: print('Type of variable is not string')
Output:
Type of variable is string
Here we fetched the type of variable sample_text and compared it with str. As sample_text actually contains the string, so our condition passed. It confirms that the type of variable sample_text is string.
Now let’s checkout a negative example,
Example 2:
sample_num = 11 if type(sample_num) == str: print('Type of variable is string') else: print('Type of variable is not string')
Output:
Type is variable is not string
Here we fetched the type of variable sample_num and compared it with str. As sample_num actually contains a number, so our condition returned False. It confirms that the type of variable sample_num is not string.
Check if type of a variable is string in python by comparing types
In the both the previous solution we hard coded the string class. But we can do without hard coding too i.e. by comparing the type of given variable and type of an empty string i.e.
sample_text = "Hello" if type(sample_text) == type(""): print('Type of variable is string') else: print('Type of variable is not string')
Output:
Type of variable is string
If both the types returned by type() functions are same, it means that our variable is of type string only.
Let’s take a negative example,
sample_num = 11 if type(sample_num) == type(""): print('Type of variable is string') else: print('Type of variable is not string')
Output:
Type of variable is not string
The complete example is as follows,
def main(): print('*** Check if type of a variable is string in python using isinstance() ***') print('Example 1:') sample_text = "Hello" # Check if the type of a variable is string if isinstance(sample_text, str): print('Type of variable is string') else: print('Type is variable is not string') print('Example 2:') sample_num = 11 # Check if the type of a variable is string if isinstance(sample_num, str): print('Type of variable is string') else: print('Type is variable is not string') print('*** Check if type of a variable is string in python using type() ***') print('Example 1:') sample_text = "Hello" if type(sample_text) == str: print('Type of variable is string') else: print('Type of variable is not string') sample_num = 11 if type(sample_num) == str: print('Type of variable is string') else: print('Type of variable is not string') print('*** Check if type of a variable is string in python by comparing types ***') print('Example 1:') sample_text = "Hello" if type(sample_text) == type(""): print('Type of variable is string') else: print('Type of variable is not string') sample_num = 11 if type(sample_num) == type(""): print('Type of variable is string') else: print('Type of variable is not string') if __name__ == '__main__': main()
Output:
*** Check if type of a variable is string in python using isinstance() *** Example 1: Type of variable is string Example 2: Type is variable is not string *** Check if type of a variable is string in python using type() *** Example 1: Type of variable is string Type of variable is not string *** Check if type of a variable is string in python by comparing types *** Example 1: Type of variable is string Type of variable is not string
Latest Video Tutorials