判断字符串是否是数值的传统代码参考:

sInput=input("请输入数值[整数或小数]:")
sInput2=""
bErr=False
cSign=""
if(sInput.startswith("+")):
sPlus=sInput.count("+")
if(sPlus==1):
sInput2=sInput.lstrip("+")
cSign="+"
else:
bErr=True
elif(sInput.startswith("-")):
sSub=sInput.count("-")
if(sSub==1):
sInput2=sInput.lstrip("-")
cSign="-"
else:
bErr=True
else:
sChar=sInput[0:1]
if(sChar.isdigit()==False):
bErr=True
else:
sInput2=sInput if(bErr):
print("数值格式不正确.")
elif(sInput2.isdigit()):
print("数值格式正确.")
else:
pointNum=sInput2.count(".")
if(pointNum==1):
s2=sInput2.replace(".","")
if(s2.isdigit()):
print("数值格式正确.")
else:
print("数值格式不正确.")
else:
print("数值格式不正确.")
if(bErr==False):
print(cSign+sInput2)

采用正则表达式来判断字符串是否是数值的参考代码:

import re

strInput = '310.1'
strList = re.findall('^[\-\+]?\d+\.?\d+$',strInput)
print(strList)
strInput2 = 'aa310.0.1'
strList2 = re.search('[\-\+]?\d+\.?\d+',strInput2)
print(strList2.group()) strInput3 = '310.0.1'
strList3 = re.match('[\-\+]?\d+\.?\d+',strInput3)
print(strList3.group()) strInput4 = '310.13'
strList4 = re.match('^[\-\+]?\d+\.?\d+$',strInput4) if(strList4):
res=strList4.group()
print(res)
else:
print("Err")strInput5 = '310.01a'
strInput5 = '310.13'
strList5 = re.fullmatch('[\-\+]?\d+\.?\d+',strInput5)
if(strList5):
print(strList5.group())
else:
print("Err")

[Python]判断字符串是否是数值,可以采用正则表达式技术来进行输入检测处理的更多相关文章

  1. python判断字符串

    python判断字符串 s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小 ...

  2. python判断字符串是否为空的方法s.strip()=='' if not s.strip():

    python 判断字符串是否为空用什么方法? 复制代码 s=' ' if s.strip()=='':     print 's is null' 或者 if not s.strip():     p ...

  3. python 判断字符串中是否只有中文字符

    python 判断字符串中是否只有中文字符 学习了:https://segmentfault.com/q/1010000007898150 def is_all_zh(s): for c in s: ...

  4. Python判断字符串编码以及编码的转换

    转自:http://www.cnblogs.com/zhanhg/p/4392089.html Python判断字符串编码以及编码的转换 判断字符串编码: 使用 chardet 可以很方便的实现字符串 ...

  5. python判断字符串中是否包含子字符串

    python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1:     print('存在') else:     print('不存在' ...

  6. python判断字符串是否是json格式方法分享

    python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例:   ...

  7. Java判断字符串是否是数值

    判断一个字符串是否是数值,可以用正则表达式来判断.更简单的方法是把字符串转换成Float或者Double,然后捕捉NumberFormatException错误,如果有错误,就说明不是一个数值,如果没 ...

  8. Python判断字符串是否为字母或者数字

    严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...

  9. python判断字符串是否为空和null

    1.使用字符串长度判断 len(s==0)则字符串为空 test1 = '' if len(test1) == 0: print('test1为空串') else: print('test非空串,te ...

  10. python判断字符串,str函数isdigit、isdecimal、isnumeric的区别

    s为字符串s.isalnum() 所有字符都是数字或者字母s.isalpha() 所有字符都是字母s.isdigit() 所有字符都是数字s.islower() 所有字符都是小写s.isupper() ...

随机推荐

  1. mysql 创建账号并授权

    1.mysql查看所有账号信息 mysql> select user,host from mysql.user; 2.创建账号 mysql> creat user 'glasssix' I ...

  2. mybatis-关联查询3-自关联查询

    一对多的方式处理 查询指定栏目的所有子孙栏目 查询指定栏目及其所有子孙栏目 多对 一的方式处理

  3. sdp安装及实例

    环境: sdpserver:192.168.1.160 sdpclient:192.168.1.161 安装 yum install gcc gcc-c++ libpcap* libtool* wge ...

  4. centos8修改更新迁徙到Stream方法

    迁徙到Stream 此方案比较简单,输入以下命令即可: wget 'http://mirror.centos.org/centos/8-stream/BaseOS/x86_64/os/Packages ...

  5. vue 安装脚手架后配置自动打开浏览器

    打开config目录下的index.js文件,将autoOpenBrowser: false,改为autoOpenBrowser: true,即可 autoOpenBrowser: true

  6. Python学习笔记调式之抛出异常

    随笔记录方便自己和同路人查阅. #------------------------------------------------我是可耻的分割线--------------------------- ...

  7. 城壁 (Rampart)

    题意简述 给定一张 $H \times W $ 的网格图,其中有 \(P\) 个被标记的点,求边长为 \(L\) 或以上的正方形的个数,要求正方形的边不得经过被标记的点. \(1 \le H,W \l ...

  8. java 动手动脑 方法重载

    如下代码://MethodOverload.java //Using overloaded methods package HJssss; public class zhuce { public st ...

  9. el-scrollbar 饿了么滚动条不出现 bug

    el-scrollbar 饿了么滚动条不出现 bug 解决方法:el-scrollbar 的直接子元素样式需要设置为 display: inline-block;

  10. k8s master高可用

    每台master都要部署haproxy,keepalived keepalived 配置文件:! Configuration File for keepalivedglobal_defs { rout ...