# Casting

# How to get Type of some variables

  • uses function type(?)
a = 10
print(type(a))

a = 10.0
print(type(a)) 

a = "10"
print(type(a))
1
2
3
4
5
6
7
8

OUTPUT

<class 'int'> <class 'float'> <class 'str'>

# Casting

  • Example 1
a = str(10)
print(type(a))

a = str(10.0)
print(type(a)) 

a = int("10")
print(type(a))

a = float("10")
print(type(a))
1
2
3
4
5
6
7
8
9
10
11
output

OUTPUT

<class 'str'> <class 'str'> <class 'int'> <class 'float'>

  • Example 2
a = 10
a = str(a)
print(type(a))
1
2
3
output

OUTPUT

<class 'str'>

  • Example 3
a = 10
b = str(a)
print(type(a))
print(type(b))
1
2
3
4
output

OUTPUT

<class 'int'> <class 'str'>

  • Example 4
a = 4
a = str(a)
b = "5"
print(a+b)
print(type(a))
print(type(b))
1
2
3
4
5
6
output

OUTPUT

45 <class 'str'> <class 'str'>

  • Example 5
a = 4
b = "5"
b = int(b)
print(a+b)
print(type(a))
print(type(b))
1
2
3
4
5
6
output

OUTPUT

9 <class 'int'> <class 'int'>

  • Example 6
a = 4
b = "5"
print(str(a)+b)
print(type(a))
print(type(b))
1
2
3
4
5
output

OUTPUT

45 <class 'int'> <class 'str'>

  • Example 7
a = 4
b = "5"
print(a+int(b))
print(type(a))
print(type(b))
1
2
3
4
5
output

OUTPUT

9 <class 'int'> <class 'str'>

# Exercise Book 04.Casting