Python Type Function with Examples

Python provides a lot of different types for programming purposes. While using these structs we may need to decide which variable or identifier is which type. Python provides builtin type function in order to decide given identifier types. We just need to provide the identifier to the type function.

Identify Type

We will provide our identifier or variable like below and type function will return related type.

1

2

3

name="ismail"

type(name)

#<class 'str'>

We can see that name is a string object or class type

Check Type

We can check type returned types with is keyword. We will first provide type function and its parameter identifier or variable and than put is and as the latest type we want to check. In this example we will check if given identifier is string. This will return a boolean result like True or False .

1

2

3

name="ismail"

type(name)isstr

#True

Is List

We can check whether given identifier or variable is a list like below.

1

2

3

mylist=['test']

type(mylist)islist

#True

Is Dictionary

We can check whether given identifier or variable is a dictionary like below.

1

2

3

mydict={'a':'b','c':'d'}

type(mydict)isdict

#True

Is String

We can check whether given identifier or variable is a string like below.

1

2

3

name="ismail"

type(name)isstr

#True

Is Integer

We can check whether given identifier or variable is a integer like below.