**Typecasting in python** also called as Type Coercion.**What is Type Casting?****Type casting Definition**: Converting One data type to another data type is known as Type Casting.

Example: – int to float or float to int

Inbuilt Functions for Type Casting in python

- int function
- float function
- complex function
- bool function
- str function

## Int Function

**Python int**: -In python, To convert any type to int type int() is used.

Python float to int Example: – int(123.456) => 123**You cannot convert complex data type to int type.**

Example: – int(10+20j) => Error**You can convert boolean to int type.**

Example: – int(True) => 1

Because internally Boolean values True and False are treated as int value 1 and 0.**You cannot convert string type to int type.**

python convert string to int

Example: – int(‘Tech’) => Error

But, int(“10”) => 10

Because string is internally treated as int so we will get 10.

Also Check this, int(“10.5”) => Error

If you want to convert string to int, then the string value must be in base 10 that is int value only.

Example: – int(“111”) => 111**You also cannot convert binary values to int**

Example: – int(“B111”) => Error

## Float Function

**Python float function used to convert any type to float data type.**

Example: – float(10) => 10.0**You cannot convert complex data type to float type**

Example: – float(10+20j) => Error**You can convert boolean to float type.**

Example: – float(True) => 1.0

Because internally Boolean values True and False are treated as int value 1 and 0.**You cannot convert string type to int type.**

Example: – float(“tech”) => Error

But, if you want to convert string to int then string value must be int or float.

Example: – float(“10”) => 10.0 or float(“10.5”) => 10..5**You cannot convert binary type to float type.**

Example:- float(“b111”) => Error

## Complex Function

- Python complex function used to convert any data type to complex type.
**Complex Functions have 2 Forms**

1. Complex(x)

2. Complex(x,y)- Form 1 for complex function
**Complex(x) => x+0j**

In form 1 we only pass one argument (Real part)

Example :- complex(10) => 10 + 0j**You can pass any type of data to complex(x) function except string and binary.**

Example:-

complex(10.5) => 10.5+0j

complex(True) => 1+0j

complex(False) => 0j

complex(“ten”) => Error

But, you can pass int and float value as string to complex function as

complex(“10”) => 10+0j

complex(“10.5”) => 10.5+0j

You cannot pass binary value.

complex(“0b111”) => Error **Form 2 for complex function**

Complex(x,y) => x+yj**In form 2 we only pass two argument (Real part and imaginary part)**

Example :- complex(10,20) => 10 +2 0j

You can pass any type of data to complex(x,y) function.

Example:-

complex(10,20.5) => 10+20.5j

complex(True,False) => 1+0j**You cannot pass string type value**

complex(“ten”,”ten”) => Error

But, you can pass int and float value as string to complex function as

complex(“10”,”20”) => Error

Because, complex cannot take second argument if first is string. Complex() can have only one string so, use first form as

complex(“10”) => 10 + 0j

## Boolean Function

**Python Boolean function used to convert any type to boolean type.****For int argument 0 means False and non zero means True.**

Example: – bool(0) => False

bool(10) => True

bool(-10) => True**You can convert float value also**

If total number value 0(0.0) then it is treated as False and for other case True.

Example: – bool(0.0) => False

bool(0.1) => True

bool(0.01) => True**For Complex number**

Example: – bool(10+20j) => True

In this, both real and imaginary parts are zero then False, else (id\f one or both value are non zero the True)

Example: – bool(0+0j) => False)

Bool(0+1j) => True**For String arguments**

If argument is empty string then it is treated as False otherwise True.

Example: – bool(‘‘) => False

bool(“Tech”) => True

bool(“ “) => True ( Space is also one character)**Note**: – bool() Never gives error for any type of data, it gives either True or False.

## String Function

**Python string function is used to convert any type to data to string type.****For this python provides str()**- Example:- str(!0) => 10
**You can convert any type of data to str type. There is no restriction for str() type.**- Python string to float :- str(10.5) => 10.5

str(True) => True

str(10+20j) => 10+20j

## Leave a Reply