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
Release: 2019-05-25 09:47:48
Original
3721 people have browsed it

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    
'''
Copy after login

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
   '''
Copy after login

str(num) Convert integers and floating point types into strings

  num = 123;    
  mystr = str(num);    
  print ("%s" % mystr);    
  ''' 输出 123   '''
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template