7️⃣ Type Casting
Type casting is converting datatype of a variable into another datatypes as per requirements.
Type casting of numeric data
a) Integer/float to string conversion:
We can convert integer variable into string by using str() method on it.
💻 Example:
1 2 3 4 5 6 7 8 9 10 | num = 100
print(type(num))
num2 = str(num)
print(num2)
print(type(num2))
marks = 25.7
print(type(marks))
marks2 = str(marks)
print(marks2)
print(type(marks2))
|
✅ Output:
<class 'int'>
100
<class 'str'>
<class 'float'>
'25.7'
<class 'str'>
b) string to integer conversion:
We can use int() method on string variable to convert it into integer datatype, provided the string must contains the only numbers.
💻 Example:
1 2 3 4 | num_string = '354'
num = int(num_string)
print(num)
print(type(num))
|
✅ Output:
354
<class 'int'>
c) string to float conversion:
float() method converts a variable into float datatype provided it should contain only numeric information.
💻 Example:
1 2 3 4 | float_string = '125.30'
float_num = float(float_string)
print(float_num)
print(type(float_num))
|
✅ Output:
125.3
<class 'float'>
d) convert float string into integer:
There is int() method to convert the string into integer datatype, however the string must contains whole number.
If the string contains fractional number, you can not directly use the int method to convert that string into integer it will give ValueError.
First convert the fractional number string into float, then convert the float value into integer as follows:
💻 Example:
1 2 3 4 5 | float_str = '225.75'
float_num= float(float_str)
int_num = int(float_num)
print(int_num)
print(type(int_num))
|
✅ Output:
225
<class 'int'>
Type casting of sequential data
a) convert list into tuple:
Python has tuple() method to convert sequential data variable list or set into tuple.
💻 Example:
1 2 3 4 5 6 7 8 9 10 | a = [1,2,3,4,5]
print(type(a))
b = tuple(a)
print(b)
print(type(b))
x = {1,2,3,4,5}
print(type(x))
y = tuple(x)
print(y)
print(type(y))
|
✅ Output:
<class 'list'>
(1,2,3,4,5)
<class tuple'>
<class 'set'>
(1,2,3,4,5)
<class tuple'>
b) convert tuple into list:
list() method converts sequential variable tuple or set into list datatype.
💻 Example:
1 2 3 4 5 6 7 8 9 10 | tup1 = ('a', 'b', 'c')
print(type(tup1))
list1 = list(tup1)
print(list1)
print(type(list1))
set1 = {1,2,3,4,5}
print(type(set1))
list1 = list(set1)
print(list1)
print(type(list1))
|
✅ Output:
<class 'tuple'>
['a', 'b', 'c]
<class 'list'>
<class 'set'>
[1,2,3,4,5]
<class 'list'>
c) convert tuple/ list into set:
We can convert sequential data (tuple/list) into set by using set() method.
The sequential data elements which are converted into set won’t be sequential or as per the order of list or tuple.
If list/tuple contains duplicate elements, the set() method only keep unique elements by removing duplicate from them.
💻 Example:
1 2 3 4 5 6 7 8 9 10 | list1 = [1,1,2,2,4,5]
print(type(list1))
set1 = set(list1)
print(set1)
print(type(set1))
tup1 = (1, 44, 2, 2)
print(type(tup1))
set1 = set(tup1)
print(set1)
print(type(set1))
|
✅ Output:
<class 'list'>
{1,2,4,5}
<class 'set'>
<class 'tuple'>
{2,44,1}
<class 'set'>
d) convert string into list:
We can also convert string into list using split() method on string.
split() takes spaces as separator by default and splits the characters separated by space inside string in to list.
You can also specify separator to split method on which you want the string characters to be splitted in to list.
💻 Example:
1 2 3 4 | my_string = "Hello how are you?"
my_list = my_string.split()
print(type(my_list))
print(my_list)
|
✅ Output:
<class list'>
['Hello', 'how', 'are', 'you?']
💻 Example:
1 2 3 4 | divisions = "div A,div B,div C,div D"
div_list = divisions.split(',')
print(type(div_list))
print(div_list)
|
✅ Output:
<class 'list'>
['div A', 'div B', 'div C', 'div D']
Be prepared with below questions prior to your interview !
Frequently asked Interview Questions
What is type casting in Python?
What is the purpose of type casting?
Can we convert a string into list? How?