Home >Backend Development >Python Tutorial >How to parse a string into a floating point number or integer
In Python programming, it is often necessary to convert strings to each other
The following methods can be used to convert data types in Python:
int(str) The function converts a string that conforms to the integer specification into an int type.
num2 = "123"; num2 = int(num1); print("num2: %d" % num2); ''' 输出 num2: 123 '''
float(str) The function converts a string that conforms to the floating point specification into a float type.
num1 = "123.12"; num2 = float(num1); print("num2: %f" % num2); ''' num2: 123.120000 '''
str(num) Convert integers and floating point types into strings
num = 123; mystr = str(num); print ("%s" % mystr); ''' 输出 123 '''
The above is the detailed content of How to parse a string into a floating point number or integer. For more information, please follow other related articles on the PHP Chinese website!