Matt Ellen
0
Q:

python get type

print(type(x))
12
v = 10
type(v)
# <type 'int'>
16
>>>foo = "Hello, World!"
>>>isinstance(foo, str) # isinstance(<input>, <type>)
True
>>>isinstance(foo, int)
False
8
print(type('string'))
# <class 'str'>

print(type('string') is str)
# True
2
str = "Hello"
type(str)
4
#This is able to tell the user what the type of an object is
string_example = "string"
int_example = 1
float_example = 1.1
type_of_string = type(string_example)
#<class 'str'>
type_of_int = type(int_example)
#<class 'int'>
type_of_float = type(float_example)
#<class 'float'>
1
# Python code for type() with a name,  
# bases and dict parameter 
  
o1 = type('X', (object,), dict(a='Foo', b=12)) 
  
print(type(o1)) 
print(vars(o1)) 
  
class test: 
      a = 'Foo'
  b = 12
    
o2 = type('Y', (test,), dict(a='Foo', b=12)) 
print(type(o2)) 
print(vars(o2)) 
0
## To get the type of a variable, use 'type()'
type("Hello World!")
## Output:
## str
2

New to Communities?

Join the community