zoukankan      html  css  js  c++  java
  • 判断类型:数字、字母、空格等

    str_1 = "123"
    str_2 = "Abc"
    str_3 = "123Abc"
     
    #用isdigit函数判断是否数字
    print(str_1.isdigit())
    Ture
    print(str_2.isdigit())
    False
    print(str_3.isdigit())
    False
     
    #用isalpha判断是否字母
    print(str_1.isalpha())   
    False
    print(str_2.isalpha())
    Ture   
    print(str_3.isalpha())   
    False
     
    #isalnum判断是否数字和字母的组合
    print(str_1.isalnum())   
    Ture
    print(str_2.isalnum())
    Ture
    print(str_1.isalnum())   
    Ture
    注意:如果字符串中含有除了字母或者数字之外的字符,比如空格,也会返回False。
     
     
    import string
    s = raw_input('input a string: ')
    letters = 0
    space = 0
    digit = 0
    others = 0
    for c in s:
        if c.isalpha():
           letters += 1
        elif c.isspace():
           space += 1
        elif c.isdigit():
           digit += 1
        else:
           others += 1
    print 'char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others)
     
     
  • 相关阅读:
    GCC编绎详解
    GUN C/C++ __attribute__ 用法 转
    rust 参考的资料 转
    Eclipse环境安装rust
    GNU Debugger for Windows----GDB
    minGW cygwin gnuwin32
    tdm-gcc
    GNU tools
    The MinGW and mingw-w64 projects.----GCC
    crosstool-NG
  • 原文地址:https://www.cnblogs.com/myshuzhimei/p/11753884.html
Copyright © 2011-2022 走看看