How to determine a Python variable's type? How to determine a Python variable's type? python python

How to determine a Python variable's type?


Use the type() builtin function:

>>> i = 123>>> type(i)<type 'int'>>>> type(i) is intTrue>>> i = 123.456>>> type(i)<type 'float'>>>> type(i) is floatTrue

To check if a variable is of a given type, use isinstance:

>>> i = 123>>> isinstance(i, int)True>>> isinstance(i, (float, str, set, dict))False

Note that Python doesn't have the same types as C/C++, which appears to be your question.


You may be looking for the type() built-in function.

See the examples below, but there's no "unsigned" type in Python just like Java.

Positive integer:

>>> v = 10>>> type(v)<type 'int'>

Large positive integer:

>>> v = 100000000000000>>> type(v)<type 'long'>

Negative integer:

>>> v = -10>>> type(v)<type 'int'>

Literal sequence of characters:

>>> v = 'hi'>>> type(v)<type 'str'>

Floating point integer:

>>> v = 3.14159>>> type(v)<type 'float'>


It is so simple. You do it like this.

print(type(variable_name))


matomo