Home >Backend Development >Python Tutorial >How to check variable type in python

How to check variable type in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-11 14:32:1215620browse

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 &#39;A&#39;>
>>>type(A)
<class &#39;type&#39;>
>>>type(A())
<class &#39;A&#39;>
>>>a=2
>>>type(a)
<class &#39;int&#39;>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn