Python exercises-Determine if the input string is a decimal type

Source: Internet
Author: User

Write a string that can judge the input is not a decimal type
1, determine whether the number of decimal points is 1 count
2, determine if the decimal right is an integer isdigit
3, judging the left of the decimal point 1, the integer isdigit,2 If it is a negative integer, take the minus sign to the right, if it is an integer
defIs_float (s): s=Str (s)ifS.count ('.') ==1: new_s=s.split ('.') Left_num=New_s[0] Right_num=new_s[1]        ifright_num.isdigit ():ifleft_num.isdigit ():returnTrueelifLeft_num.count ('-') ==1 andLeft_num.startswith ('-'): Tmp_num=left_num.split ('-') [-1]               iftmp_num.isdigit ():returnTruereturnFalsePrint(Is_float ('-9.6')) #调用函数


< Span style= "COLOR: #6897bb" >

Python Exercise-judging if the input string is a decimal type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.