zoukankan      html  css  js  c++  java
  • 基本数据类型(数字和字符串)

    一,数字

    整型(int):如(年级,年纪,等级,身份证号,qq号,手机号

    level=10
    浮点型(float):如(身高,体重,薪资,温度,价格
    height=1.81
    salary=3.00

    常用操作:

    #is数字系列
    #在python3中
    num1=b'4' #bytes
    num2=u'4' #unicode,python3中无需加u就是unicode
    num3='四' #中文数字
    num4='Ⅳ' #罗马数字
    
    #isdigt:bytes,unicode
    print(num1.isdigit()) #True
    print(num2.isdigit()) #True
    print(num3.isdigit()) #False
    print(num4.isdigit()) #False
    
    
    #isdecimal:uncicode
    #bytes类型无isdecimal方法
    print(num2.isdecimal()) #True
    print(num3.isdecimal()) #False
    print(num4.isdecimal()) #False
    
    #isnumberic:unicode,中文数字,罗马数字
    #bytes类型无isnumberic方法
    print(num2.isnumeric()) #True
    print(num3.isnumeric()) #True
    print(num4.isnumeric()) #True
    
    
    #三者不能判断浮点数
    num5='4.3'
    print(num5.isdigit())#False
    print(num5.isdecimal())#False print(num5.isnumeric())#False ''' 总结: 最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景 如果要判断中文数字或罗马数字,则需要用到isnumeric ''' #is其他 print('===>') name='egon123' print(name.isalnum()) #字符串由字母和数字组成True print(name.isalpha()) #字符串只由字母组成False print(name.isidentifier())#True print(name.islower())#True print(name.isupper())#False print(name.isspace())#False print(name.istitle())#False

    二,字符串

    定义:包含在引号(单,双,三)里面,由一串字符串组成。

    用途:保存描述性的内容,比如:姓名,性别,地址,学历,密码等;

    取值:首先要明确,字符串整体就是一个值,只不过特殊之处在于:python中没有字符类型,字符串是由一串字符组成,想取出字符串中的字符,也可以按照下标的方式取得:

    name:取得是字符串整体的那一个值
    name[1]:取得是第二位置的字符

    常用操作:

        移除空白strip

        切分split

        长度len

        索引

        切片

            其他操作(包括常用)

    #strip移除空白
    name='*wxp**'
    print(name.strip('*'))#--》wxp
    print(name.lstrip('*'))#==》wxp**
    print(name.rstrip('*'))#-->*wxp
    #startswith,endswith判断字符串开头结尾中的字符
    name='Hello_word'
    print(name.endswith('rd'))#==>True
    print(name.startswith('he'))#==>False
    #replace替换
    name='I have a dream ,my dream is go to sea'
    print(name.replace('dream','梦想',1))#==》I have a 梦想 ,my dream is go to sea
    #format的三种玩法
    res='{} {} {}'.format('egon',18,'male')
    print(res)#-->egon 18 male
    res='{1} {0} {1}'.format('egon',18,'male')
    print(res)#-->18 egon 18
    res='{name} {age} {sex}'.format(sex='male',name='egon',age=18)
    print(res)#-->egon 18 male
    #find,rfind,index,rindex,count
    name='egon say hello'
    print(name.find('o',1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引
    #print(name.index('e',2,4)) #同上,但是找不到会报错
    print(name.count('e',1,3)) #顾头不顾尾,如果不指定范围则查找所有
    #split
    name='root:x:0:0::/root:/bin/bash'
    print(name.split(':')) #默认分隔符为空格-->['root', 'x', '0', '0', '', '/root', '/bin/bash']
    name='C:/a/b/c/d.txt' #只想拿到顶级目录
    print(name.split('/',1))#==>>['C:', 'a/b/c/d.txt']
    name='a|b|c'
    print(name.rsplit('|',1)) #从右开始切分==>['a|b', 'c']
    #join
    tag=' '
    print(tag.join(['egon','say','hello','world'])) #可迭代对象必须都是字符串==>>egon say hello world
    #center,ljust,rjust,zfill
    name='egon'
    print(name.center(30,'-'))#==>>-------------egon-------------
    print(name.ljust(30,'*'))#==>>egon**************************
    print(name.rjust(30,'*'))#==>>**************************egon
    print(name.zfill(50)) #用0填充==>>0000000000000000000000000000000000000000000000egon
    #expandtabs
    name='egon hello'
    print(name)#==>egon hello
    print(name.expandtabs(1))#==>egon hello
    #lower,upper大小写转换
    name='eGon'
    print(name.lower())#==>egon
    print(name.upper())#==>EGON
    #captalize,swapcase,title
    name='helloWORLD'
    print(name.capitalize()) #首字母大写==>Helloworld
    print(name.swapcase()) #大小写翻转==>HELLOworld
    msg='egon say hi'
    print(msg.title()) #每个单词的首字母大写==>Egon Say Hi

      

  • 相关阅读:
    IO操作之BIO、NIO、AIO
    IO之Socket网络编程
    this.getClass()和super.getClass()得到的是同一个类
    经济增长的背后
    SVN分支创建与合并
    .net类库里ListView的一个BUG
    VS2010调试技巧
    用C#代码编写的SN快速输入工具
    请教如何改善C#中socket通信机客户端程序的健壮性
    利用WebClient实现对Http协议的Post和Get对网站进行模拟登陆和浏览
  • 原文地址:https://www.cnblogs.com/wxp5257/p/7195489.html
Copyright © 2011-2022 走看看