Datatype conversion

Once in a while you will want to convert data type of one type to another type. Data type conversion is also known as Type casting.

Converting int to float

To convert int  to float  you need to use float() function.

Converting float to int

To convert float  to int  you need to use int() function.

Converting string to int

You can also use int()  to convert string  to int

Note: If string contains non numeric character then int()  will throw ValueError.

Converting number to string

To convert number  to string  you need to use str()  function.

Rounding numbers

To round numbers you need to use round()  function.

Syntax: round(number[, ndigits])

Next we will cover control statements.

Leave a Reply

4 Comments on "Datatype conversion"

Notify of
avatar

Sort by:   newest | oldest | most voted
xexanos
Guest
xexanos
1 year 3 months ago

Hey,
in your first example you write “To convert float to int” but it should be “To convert int to float”.

vishal
Guest
vishal
1 year 4 months ago

hey admin,
there might be something wrong in first example. You are using i=10 for input, and after conversion from int to float, it’s showing 14.0

wpDiscuz