zoukankan      html  css  js  c++  java
  • isnumeric()方法

    isnumeric()方法

    描述

    isnumeric() 方法检测字符串是否只由数字组成。这种方法是只针对unicode对象。

    注:定义一个字符串为Unicode,只需要在字符串前添加 'u' 前缀即可,具体可以查看本章节例子。

    语法

    isnumeric()方法语法:

    str.isnumeric()

    参数

    • 无。

    返回值

    如果字符串中只包含数字字符,则返回 True,否则返回 False

    实例

    以下实例展示了isnumeric()方法的实例:

    str = "runoob2016"  
    print (str.isnumeric())
    
    str = "23443434"
    print (str.isnumeric())
    
    # 结果为
    # False
    # True

    s.isdigit、isdecimal 和 s.isnumeric 区别

    isdigit()

    True: Unicode数字,byte数字(单字节),全角数字(双字节)

    False: 汉字数字,罗马数字,小数

    Error: 无

    isdecimal()

    True: Unicode数字,,全角数字(双字节)

    False: 罗马数字,汉字数字,小数

    Error: byte数字(单字节)

    isnumeric()

    True: Unicode 数字,全角数字(双字节),汉字数字

    False: 小数,罗马数字

    Error: byte数字(单字节)

    num = "1"  #unicode
    num.isdigit()   # True
    num.isdecimal() # True
    num.isnumeric() # True
    
    num = "1" # 全角
    num.isdigit()   # True
    num.isdecimal() # True
    num.isnumeric() # True
    
    num = b"1" # byte
    num.isdigit()   # True
    num.isdecimal() # AttributeError 'bytes' object has no attribute 'isdecimal'
    num.isnumeric() # AttributeError 'bytes' object has no attribute 'isnumeric'
    
    num = "IV" # 罗马数字
    num.isdigit()   # False
    num.isdecimal() # False
    num.isnumeric() # False
    
    num = "" # 汉字
    num.isdigit()   # False
    num.isdecimal() # False
    num.isnumeric() # True

     

  • 相关阅读:
    nowcoderD Xieldy And His Password
    Codeforces681D Gifts by the List
    nowcoder80D applese的生日
    Codeforces961E Tufurama
    Codeforces957 Mahmoud and Ehab and yet another xor task
    nowcoder82E 无向图中的最短距离
    nowcoder82B 区间的连续段
    Codeforces903E Swapping Characters
    Codeforces614C Peter and Snow Blower
    Codeforces614D Skills
  • 原文地址:https://www.cnblogs.com/xiaohei001/p/10123410.html
Copyright © 2011-2022 走看看