超人口袋——Python篇

Python《字符串高效判断中文、英文、数字或者其他字符等》

2018-02-24  本文已影响68人  超人猿
序言:

     总结Python在字符串方面如何高效判断中文、英文、数字或者其他字符的经验, 不断更新~

判断中文

st = "我爱中国abc"
for s in st:
     if s >= u'\u4e00' and s<=u'\u9fa5':
          print("%s 是中文" %s)

效果图:


1.png

判断英文

st = "我爱中国I love China"
for s in st:
     if (s >= u'\u0041' and s <= u'\u005a') or (s >= u'\u0061' and s <= u'\u007a'):
          print("%s 是英文" %s)

效果图:


2.png

判断数字

st = "我爱中国I love China 520"
for s in st:
     if s.isdigit():
          print("%s 是数字" %s)

效果图:


3.png

判断空格

st = "我爱中国I love China 520"
for s in st:
     if s.isspace():
          print("%s 是空格" %s)

效果图:


4.png

判断特殊字符,可基于以上情况的else

上一篇下一篇

猜你喜欢

热点阅读