(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

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.

1 2 3 | >>> i = 10 >>> float(i) 10.0 |

## Converting float to int

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

1 2 3 | >>> f = 14.66 >>> int(f) 14 |

## Converting string to int

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

1 2 3 | >>> s = "123" >>> int(s) 123 |

**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.

1 2 3 4 5 6 | >>> i = 100 >>> str(i) "100" >>> f = 1.3 str(f) '1.3' |

## Rounding numbers

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

**Syntax**: round(number[, ndigits])

1 2 3 | >>> i = 23.97312 >>> round(i, 2) 23.97 |

Next we will cover control statements.

#### Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

vishalhey 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

adminPost authorcode updated. Thanks

xexanosHey,

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

adminPost authorCode updated

Alex MakhalaneHi Admin

1.

Converting Number to Stringdoes not work. The following error is displayed:“Traceback (most recent call last):

File “”, line 1, in

TypeError: ‘str’ object is not callable”

2.

Rounding Numbersrounds off to 2 decimal places regardless of the number in the second parameter.round(i, 2), round(i, 3), round(i, 4) etc. all result in 24.0

I am using Python 3.9.3 (v3.6.3:2c5fed8)

Regards

Alex MakhalaneSorry, I meant Python 3.6.3.

JacobHere is how to convert number to string.

>>>

>>> str(3.14)

'3.14'

>>> str(100)

'100'

>>>

Secondly, if you are not getting the required result using

`round()`

function, try`format()`

function.