Reading Time: 1 minutes
Changing Data Types - Type Casting in Python
Python identifiers do not carry any information about their type while declaration. As soon as a value is assigned to an identifier, Python determines the data type of the identifier, and gives it its own set of associated functions depending on the data type. There are instances when you need to explicitly convert one data type into another. For this purpose, Python provides numerous builtin functions. For example, in order to convert a string identifier into an integer, we can use the int() function.
>>> oneTwoThree = '123' >>> type(oneTwoThree) <class 'str'> >>> number123 = int(oneTwoThree) >>> type(number123) <class 'int'>
Similarly, Python provides these functions: str(), float(), complex(), bool(), chr(), bin(), oct() & hex(). You can use the builtin help function to know how to use these.