Home >Backend Development >Python Tutorial >How to parse a string into a floating point number or integer

How to parse a string into a floating point number or integer

anonymity
anonymityOriginal
2019-05-24 17:42:243724browse

In Python programming, it is often necessary to convert strings to each other

How to parse a string into a floating point number or integer

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn