
Type Casting is a concept of converting one data type into another data type. To convert, simply use the data-type-name as a function.
There are several built-in functions to perform conversion from one data type to another.
| Function | Description |
|---|---|
| int(x [,base]) | Converts x to an integer. The base specifies the base if x is a string. |
| float(x) | Converts x to a floating-point number. |
| str(x) | Converts object x to a string representation. |
| eval(str) | Evaluates a string and returns an object. |
| list(s) | Converts s to a list. |
| tuple(s) | Converts s to a tuple. |
| set(s) | Converts s to a set. |
| dict(d) | Creates a dictionary. d must be a sequence of (key,value) tuples. |
| chr(x) | Converts an integer to a character. |
a=5.5
b=int(a) # float to int
b=int(10.9) # b will be 10
b=int("5") # string to int
a=5
b=float(a) # int to float
b=float(10) # b will be 10
b=float("5.5") # string to float
a=5 b=str(a) # int to string b=str(10) # int to string b=str(5.5) # float to string
Ad: