av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)Python教程:python判斷字符串是否為數(shù)字

以下實(shí)例通過創(chuàng)建自定義函數(shù) is_number() 方法來判斷字符串是否為數(shù)字:

更多相關(guān)內(nèi)容,請(qǐng)參考這篇文章:python判斷字符是否為字母和數(shù)字

# -*- coding: UTF-8 -*-
 
# Filename : test.py
# author by : www.runoob.com
 
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
 
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
 
    return False
 
# 測(cè)試字符串和數(shù)字
print(is_number('foo'))   # False
print(is_number('1'))     # True
print(is_number('1.3'))   # True
print(is_number('-1.37')) # True
print(is_number('1e3'))   # True
 
# 測(cè)試 Unicode
# 阿拉伯語 5
print(is_number('?'))  # True
# 泰語 2
print(is_number('?'))  # True
# 中文數(shù)字
print(is_number('四')) # True
# 版權(quán)號(hào)
print(is_number('?'))  # False

我們也可以使用內(nèi)嵌 if 語句來實(shí)現(xiàn):

執(zhí)行以上代碼輸出結(jié)果為:

False
True
True
True
True
True
True
True
False

Python isdigit() 方法檢測(cè)字符串是否只由數(shù)字組成。

Python isnumeric() 方法檢測(cè)字符串是否只由數(shù)字組成。這種方法是只針對(duì)unicode對(duì)象。


文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:python判斷字符串是否為數(shù)字
鏈接URL:http://uogjgqi.cn/article/cdgcghj.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流