-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeCasting.py
More file actions
76 lines (49 loc) · 2.05 KB
/
Copy pathtypeCasting.py
File metadata and controls
76 lines (49 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
print(len(12345))
# the above line => error =>TypeError: object of type 'int' has no len()
# The len() property is only to calculate the length of string only , wrong input gives error
# ----------- imp idea about type conversion of int to str ---------------------------------------
length = len('Jenny Khatri')
print("Your name has " + length + "characters")
# error : TypeError: can only concatenate str (not "int") to str
# To solve this issue, do one thing -> convert that int to str
print("Your name has " + str(length)+ " characters")
# the type of int converted to string
# -------------------------------------------------------------------------------------------------
# """
# int()
# float()
# str()
# Same as int was converted to string using str() vise versa is also possible using int() , float().
# """
# ----------------------------------------------------------------------------------------------------
# Take 2 inputs from the user and check their types, it is indeed surprising :
val_1 = input("enter value_1")
val_2 = input ('enter value_2')
print(type(val_1))
print(type(val_2))
# output :
# enter value_1 : 123
# enter value_2 : hello
# <class 'str'>
# <class 'str'>
# even int is shown as type str
# ----------------------------------------------------------------------------------------------------
val_1 = input ('Enter value 1')
val_2 = input ('Enter value 2')
print(val_1 + val_2)
# the output : is concatenated string not the sum
# ------------------------------------------------------------------------------------------------------
# So how to convert the string to number :
val_1 = input("enter the val_1")
val_2 = input("enter the val_2")
print(int(val_1) + int(val_2))
# ------------------------------------------------------------------------------------------
# Write a program to add digits of a number :
val = 34
strConv = str(34)
val_1 = strConv[0]
val_2 = strConv[1]
conV1 = int(val_1)
conV2 = int(val_2)
print(conV1 + conV2)
# ----------------------------------------------------------------------------------------------