Type Casting
Type Casting
In Python there are different data types, such as numbers, sequences, mappings
etc. There may be a situation where, you have the available data of one type but
you want to use it in another form. For example, the user has input a string but
you want to use it as a number. Python's type casting mechanism let you do
that.
Note that memory requirement of each data type is different. For example,
an integer object in Python occupies 4 bytes of memory, while a float object
needs 8 bytes because of its fractional part. Hence, Python interpreter doesn't
automatically convert a float to int, because it will result in loss of data. On the
other hand, int can be easily converted into float by setting its fractional part to
0.
Implicit int to float casting takes place when any arithmetic operation
on int and float operands is done.
a=True;
b=10.5;
c=a+b;
print (c);
output
11.5
Example 2
a=10
b=1.5
c=a+b
print(c)
print(type(c))
output
11.5
<class 'float'
● String to Integer
The int() function returns an integer from a string object, only if it contains a
valid integer representation.
However, if the string contains a non-integer representation, Python raises
ValueError.
Example:
print(a)
print(type(a))
a = int("10" + "01")
print(a)
print(type(a))
output
100
<Class ‘int’>
1001
a=int(“110011”,2)
print(a)
output
The string should only contain 0 to 7 digits, and the base should be 8.
a = int("20", 8)
print( a)
output
16
The string should contain only the Hexadecimal symbols i.e., 0-9 and A, B, C,
D, E or F. Base should be 16.
a = int("2A9", 16)
print(a)
output:
681
Decimal equivalent of Hexadecimal 2A9 is 681. You can easily verify these
conversions with calculator app in Windows, Ubuntu or Smartphones.
Following is an example to convert number, float and string into integer data
type:
Open Compiler
a = int(1) # a will be 1
b = int(2.2) # b will be 2
c = int("3") # c will be 3
print (a)
print (b)
print (c)
1
2
3
Python float() Function
a = float(100)
print( a)
print( type(a))
Output:
100
<class ‘float’>
The float() function returns float object from a string, if the string contains a
valid floating point number, otherwise ValueError is raised.
print (a)
print (b)
print (c)
output:
1.0
2.2
3.3
Python str() Function
The str() function has three parameters. First required parameter (or
argument) is the object whose string representation we want. Other two
operators, encoding and errors, are optional.
We shall execute str() function in Python console to easily verify that the
returned object is a string, with the enclosing quotation marks (').
a = str(10)
a '10'
print( type(a))
output:
<class ‘str’>
● Float to String
str() function converts floating point objects with both the notations of
floating point, standard notation with a decimal point separating integer and
fractional part, and the scientific notation to string object.
a=str(11.10)
print(a)
print(type(a))
a = str(2/5)
print(a)
print( type(a))
output:
11.1
<class ’str’>
0.4
<class ’str’>
In the second case, a division expression is given as argument to str() function.
Note that the expression is evaluated first and then result is converted to string.
Floating points in scientific notations using E or e and with positive or negative
power are converted to string with str() function.
a=str(10E4)
print(a)
print(type(a))
a=str(1.23e-4)
print(a)
print(type(a))
Output
100000.0
<class 'str'>
0.000123
<class 'str'>
We shall take an object each of these three sequence types and study their
inter-conversion.
a=[1,2,3,4,5] # List Object
b=(1,2,3,4,5) # Tupple Object
c="Hello" # String Object
### list() separates each character in the string and builds the list
obj=list(c)
print(obj)
### The parentheses of tuple are replaced by square brackets
obj=list(b)
print(obj)
### tuple() separates each character from string and builds a tuple of
characters
obj=tuple(c)
print(obj)
### square brackets of list are replaced by parentheses.
obj=tuple(a)
print (obj)
### str() function puts the list and tuple inside the quote symbols.
obj=str(a)
print(obj)
obj=str(b)
print( obj)
Output