Home >Backend Development >Python Tutorial >How to check variable type in python
There are two ways to view variable types in python:
isinstance() function
can be used to determine the type of a variable. It returns a Boolean value, False or True.
>>>isinstance("123",str) >>>True >>>isinstance(123,int) >>>True >>>isinstance({'123'},list) >>>False >>>isinstance(['123'],list) >>>True
Like int, float, etc. are all basic variable types. In fact, classes are also a type of variables.
type() function
does not determine the type of the variable, but directly returns the type of the variable
>>>class A(): ... pass >>>B=A() >>>type(B) <class 'A'> >>>type(A) <class 'type'> >>>type(A()) <class 'A'> >>>a=2 >>>type(a) <class 'int'>
The above is the detailed content of How to check variable type in python. For more information, please follow other related articles on the PHP Chinese website!