博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基本数据类型(数字和字符串)
阅读量:4554 次
发布时间:2019-06-08

本文共 3219 字,大约阅读时间需要 10 分钟。

一,数字

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

level=10

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

常用操作:

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

 

  

转载于:https://www.cnblogs.com/wxp5257/p/7195489.html

你可能感兴趣的文章
搭建GitLab的Hexo博客记录
查看>>
NYOJ 496 [巡回赛-拓扑排序]
查看>>
redis五种数据类型的使用
查看>>
Form表单中的onClick,onSubmit和submit
查看>>
Python-SocketServer源码
查看>>
JavaScript-基本数据类型
查看>>
CentOS 7.3 实体机启动 U 盘制作
查看>>
mysql数据库
查看>>
dede调用文章里的图片
查看>>
windows 窗体基本控件
查看>>
unix date 命令获取某日期的前一天
查看>>
dsp5509的中断系统
查看>>
VS2015 安装nuget离线包nupkg文件
查看>>
《Java编程思想》读书笔记
查看>>
x:Name vs. x:Key --摘自WPFwiki
查看>>
find与rm实现查找并删除目录或文件
查看>>
nginx替换响应内容
查看>>
在 npm 中使用 ES6 module
查看>>
RabbitMQ 远程 IP 访问 解决办法 -摘自网络
查看>>
如何离线安装Visual Studio 2017
查看>>