Python Basic

Covert Datatype in Python

Function What It Does
int(<a string of digits>) Converts a string of digits to an integer value.
float(<a string of digits>) Converts a string of digits to a floating-point value.
eval(<a string of digits>) Converts a string of datatype to datatype or expression to result.

Example

a = '123'
a = int(a)
b = '3.5'
b = float(b)
c = '478'
c = eval(c)
d = '4.8'
d = eval(d)

Kết quả: a = 123, b = 3.5, c = 478, d = 4.8

e = 'True'
e = eval(e)
f = 'None'
f = eval(f)
g = '[1, 3, 3]'
g = eval(a)
h = '{"one" : 1, "five": 5}'
eval(h)
i = '3+7'
i = eval(i)

Kết quả: e = True, f = None, g = [1, 3, 3], h = { "one" : 1, "five": 5 }, i = 10