Data type conversion python

# Software tools: Sublime Text

# Leads to question: Why do we need the data type conversion?
= A '. 1'
B = '2'
C = A + B
Print (C)
# 12 as a result, because the string is actually adding type splice
# 3 results desired, it is necessary to convert the string to an integer type
int = D (A) + int (B)
Print (D)
# 3 result

# A, is converted into integer
# 1. Convert floating point to integer type
A = 4.567
B = int (A)
Print (B)
# 4 as a result, it is converted into a floating-point type integer, rounding all fractional part , leaving only the integer part

# 2 is converted into a string type Integer
A = '1234'
b = int (A)
Print (b)
# 1234 as a result, rounding quotes
print (type (b)) # b view data type
# character string type to integer conversion process, the string can not be any non-numeric characters, otherwise an error
C = '1234.5'
D = int (C)
Print (D)
# results being given, it can not have a decimal point, letters, or other non-numeric characters

# Second, the integer / floating-point conversion to a string
# without any constraints, direct conversion
A = 123
B = 234.0
a_str STR = (A)
b_str = STR (B)
Print (a_str)
Print (type (a_str ))
Print (b_str)
Print (of the type (b_str))

# III is converted to floating point type
# 1 would be converted to floating point type Integer
# without any constraint, direct conversion
A = 2
B = a float (A)
Print (B)
Print (type (B))

# 2. Converts string type of floating type
# in the string, can not appear any character other than a decimal point, or the conversion failure
A = '1234'
B = a float (A)
Print (B)
# result 1234.0
Print (type (B))
C = '123.4' # allowed decimal point
D = a float (C)
Print (D)
# result 123.4
Print (type (D))

Guess you like

Origin www.cnblogs.com/rml1112/p/11057240.html