Python string integer and floating point conversion instances

  • 2020-12-07 04:06:55
  • OfStack

Foreword sequence kam

In programming, the conversion of strings is often used, so here is a record of how the conversion between strings and integers and floating point numbers in Python is performed.

The int(str) function converts a string that conforms to the specified integer to an int type

The float(str) function converts a string that conforms to the requirements of the floating-point type to the float type

str(num) converts integer, floating point, to string

The int(str) function converts a string that conforms to the specified integer to the int type


num2 = "123"; 
num2 = int(num1); 
print("num2: %d" % num2); 
''' 
 The output  num2: 123 
'''

The float(str) function converts a string that conforms to the requirements of the floating-point type to the float type


num1 = "123.12"; 
num2 = float(num1); 
print("num2:%f " %num2); 
''' 
num2:123.120000 
'''

str(num) converts integer, floating point, to string


num = 123; 
mystr = str(num); 
print ("%s" % mystr); 
'''  The output  123 '''

Related articles: