Python Type Function with Examples – POFTUT

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.

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 .

name="ismail" 
type(name) is str 
#True

Is List

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

mylist=['test'] 
type(mylist) is list 
#True

Is Dictionary

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

mydict={'a':'b','c':'d'} 
type(mydict) is dict 
#True

Is String

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

name="ismail"  
type(name) is str 
#True

Is Integer

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

mynum=1 
type(mynum) is int 
#True

Is Object

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

type(mynum) is object 
#False

LEARN MORE  Bash Printf Function Tutorial with Examples

Leave a Comment