python数据类型,,数字类型 和 字符串


数字类型 和 字符串类型

技术分享图片
1.bin()函数将十进制转换成而进制2.oct()函数将十进制转换成八进制3.hex()函数将十进制转换成十六进制         十六进制表示:0-9 a b c d e f4.数字类型的特性:        只能存放一个值      一经定义,不可更改     直接访问分类:整型,布尔,浮点,复数5.字符串类型    引号包含的都是字符串类型    S1=‘hello world‘  s="hello world"    s2="""hello world"""      s3=‘‘‘hello world‘‘‘    单引双引没有区别6.字符串的常用操作    strip()移除空白,也可以去除其他的字符    slipt()分割,默认以空格分割。也可以以其他的字符分割    len()长度  切片:如print(x[1:3])也是顾头不顾尾               print(x[0:5:2])#0 2 4  capitalize()首字母大写   center()居中显示例如:x=‘hello‘  print(x.center(30,‘#‘))   count():计数,顾头不顾尾,统计某个字符的个数,空格也算一个字符    endswith()以什么结尾   satrtswith()以什么开头   find()查找字符的索引位置,如果是负数,代表查找失败     index()索引    find()和index()的区别,如下图:        format()字符串格式化         1.msg=‘name:{},age:{},sex:{}‘                   print(msg.format(‘haiyan‘,18,女))        2.msg=‘name:{0},age:{1},sex:{0}‘          print(msg.format(‘aaaaaa‘,‘bbbbbb‘))        3.msg=‘name:{x},age:{y,sex:{z}‘          print(msg.format(x=‘haiyan‘,y=‘18‘,z=‘女‘))    isdigit()判断是否是数字   islower()判断是否是全部小写   isupper()判断是否是全部大写   lower()全部转换为小写   upper()全部转换为大写   isspace()判断是否是全都是空格   istitle()判断是否是标题(首字母大写)    swapcase()大小写字母翻转   join()连接   repalce()替换        msg=‘hello alex‘        print(msg.replace(‘e‘),‘A‘,1)       print(msg.replace(‘e‘),‘A‘,2)   ljust()左对齐        X=‘ABC‘    print(x.ljust(10,‘*‘))
数字类型 和 字符串类型

字符串格式化及字符串的一些方法

1.%s,%d

举例1:name=‘egon‘

    age=20

    print("my name is %s my age is %s" %(name,age))#%s既能接受字符串,也能接受数字

    print(‘my name is %s my age is %d’ %(name,age))#%d只能接受数字

举例2:用户信息的显示

while True:    name=input("name:")    age=input("age:")    sex=input("sex:")    height=input("height:")    msg=‘‘‘             ------------%s info-----------             name:%s             age:%s             sex:%s             height:%s             ------------------------------        ‘‘‘%(name,name,age,sex,heigth)    print(msg)

运行结果如下:

技术分享图片

2.字符串方法

技术分享图片

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205# name=‘egon‘ #name=str(‘egon‘)# print(type(name))#优先掌握#1.移除空白strip# msg=‘ hello ‘# print(msg)# print(msg.strip())# 移除‘*’# msg=‘***hello*********‘# msg=msg.strip(‘*‘)# print(msg)#移除左边的# print(msg.lstrip(‘*‘))#移除右边的# print(msg.rstrip(‘*‘))#用处whileTrue:name=input(‘user: ‘).strip()password=input(‘password: ‘).strip()ifname==‘egon‘andpassword==‘123‘:print(‘login successfull‘)#切分split# info=‘root:x:0:0::/root:/bin/bash‘# print(info[0]+info[1]+info[2]+info[3])# user_l=info.split(‘:‘)# print(user_l[0])# msg=‘hello world egon say hahah‘# print(msg.split()) #默认以空格作为分隔符#cmd=‘download|xhp.mov|3000‘# cmd_l=cmd.split(‘|‘)# print(cmd_l[1])# print(cmd_l[0])# print(cmd.split(‘|‘,1))#用处whileTrue:cmd=input(‘>>: ‘).strip()iflen(cmd)==0:continuecmd_l=cmd.split()print(‘命令是:%s 命令的参数是:%s‘%(cmd_l[0],cmd_l[1]))#长度len# print(len(‘hell 123‘))#索引# 切片:切出子字符串# msg=‘hello world‘# print(msg[1:3]) #1 2# print(msg[1:4]) #1 2 3# 掌握部分oldboy_age=84whileTrue:age=input(‘>>: ‘).strip()iflen(age)==0:continueifage.isdigit():age=int(age)else:print(‘must be int‘)#startswith,endswith# name=‘alex_SB‘# print(name.endswith(‘SB‘))# print(name.startswith(‘alex‘))#replace# name=‘alex say :i have one tesla,my name is alex‘# print(name.replace(‘alex‘,‘SB‘,1))# print(‘my name is %s my age is %s my sex is %s‘ %(‘egon‘,18,‘male‘))# print(‘my name is {} my age is {} my sex is {}‘.format(‘egon‘,18,‘male‘))# print(‘my name is {0} my age is {1} my sex is {0}:{2}‘.format(‘egon‘,18,‘male‘))# print(‘my name is {name} my age is {age} my sex is {sex}‘.format(# sex=‘male‘,# age=18,# name=‘egon‘))# name=‘goee say hello‘# # print(name.find(‘S‘,1,3)) #顾头不顾尾,找不到则返回-1不会报错,找到了则显示索引# # print(name.index(‘S‘)) #同上,但是找不到会报错## print(name.count(‘S‘,1,5)) #顾头不顾尾,如果不指定范围则查找所有#join# info=‘root:x:0:0::/root:/bin/bash‘# print(info.split(‘:‘))# l=[‘root‘, ‘x‘, ‘0‘, ‘0‘, ‘‘, ‘/root‘, ‘/bin/bash‘]# print(‘:‘.join(l))#lower,upper# name=‘eGon‘# print(name.lower())# print(name.upper())#了解部分#expandtabs# name=‘egon\thello‘# print(name)# print(name.expandtabs(1))#center,ljust,rjust,zfill# name=‘egon‘# # print(name.center(30,‘-‘))# print(name.ljust(30,‘*‘))# print(name.rjust(30,‘*‘))# print(name.zfill(50)) #用0填充#captalize,swapcase,title# name=‘eGon‘# print(name.capitalize()) #首字母大写,其余部分小写# print(name.swapcase()) #大小写翻转# msg=‘egon say hi‘# print(msg.title()) #每个单词的首字母大写#在python3中num0=‘4‘num1=b‘4‘#bytesnum2=u‘4‘#unicode,python3中无需加u就是unicodenum3=‘四‘#中文数字num4=‘Ⅳ‘#罗马数字#isdigt:str,bytes,unicode# print(num0.isdigit())# print(num1.isdigit())# print(num2.isdigit())# print(num3.isdigit())# print(num4.isdigit())#isdecimal:str,unicode# num0=‘4‘# num1=b‘4‘ #bytes# num2=u‘4‘ #unicode,python3中无需加u就是unicode# num3=‘四‘ #中文数字# num4=‘Ⅳ‘ #罗马数字# print(num0.isdecimal())# # print(num1.)# print(num2.isdecimal())# print(num3.isdecimal())# print(num4.isdecimal())#isnumeric:str,unicode,中文,罗马# num0=‘4‘# num1=b‘4‘ #bytes# num2=u‘4‘ #unicode,python3中无需加u就是unicode# num3=‘四‘ #中文数字# num4=‘Ⅳ‘ #罗马数字## print(num0.isnumeric())# # print(num1)# print(num2.isnumeric())# print(num3.isnumeric())# print(num4.isnumeric())#is其他# name=‘egon123‘# print(name.isalnum()) #字符串由字母和数字组成# name=‘asdfasdfa sdf‘# print(name.isalpha()) #字符串只由字母组成## name=‘asdfor123‘# print(name.isidentifier())name=‘egGon‘print(name.islower())# print(name.isupper())# print(name.isspace())name=‘Egon say‘print(name.istitle())

  

分类:python相关好文要顶关注我收藏该文技术分享图片技术分享图片技术分享图片海燕。
关注 - 31
粉丝 - 685+加关注10«上一篇:模拟exit()退出命令实现
»下一篇:列表posted on2017-07-19 16:54海燕。阅读(667) 评论(0)编辑收藏刷新评论刷新页面返回顶部发表评论

昵称:

评论内容:技术分享图片技术分享图片技术分享图片技术分享图片技术分享图片技术分享图片

退出订阅评论

[Ctrl+Enter快捷键提交]

【推荐】超50万VC++源码: 大型组态工控、电力仿真CAD与GIS源码库!
【活动】华为云12.12会员节 云产品1折起 满额送Mate20 点击抢购
【推荐】服务器100%基准CPU性能,1核1G首年168元,限时特惠!技术分享图片最新新闻
·微软2018开源大事记
·为了硬扛抖音,腾讯拉了100个短视频达人打群架
·禁令之下交易仍很活跃 “呼死你”软件为何打不死?
·百度无人车拿下天津首批路测牌照 获中国专利奖
·天文学家与阿里寻找第二地球:39光年外或有生命条件
»更多新闻...

公告

欢迎第技术分享图片个访客

菜鸟教程链接

昵称:海燕。
园龄:1年6个月
粉丝:685
关注:31+加关注
<2018年12月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

搜索

常用链接

我的随笔我的评论我的参与最新评论我的标签

我的标签

算法相关(1)

随笔分类

Ajax(2)Django(28)flask(15)Github(1)HTTP协议(1)mysql数据库(16)python相关(52)VUE(6)Web前端(16)计算机网络基础知识(3)面向对象(6)爬虫相关(8)权限管理(3)网络编程socket(14)项目相关(14)

随笔档案

2018年10月 (13)2018年9月 (6)2018年8月 (4)2018年7月 (5)2018年6月 (7)2018年5月 (14)2018年4月 (9)2018年3月 (11)2018年2月 (26)2018年1月 (50)2017年12月 (26)2017年11月 (23)2017年10月 (19)2017年9月 (27)2017年8月 (30)2017年7月 (30)

相册

微信支付(1)

最新评论

1. Re:统计s="hello alex alex hello haiyan cc haiyan com"中每个单词的个数Alex哈哈--默罕默德_c2. Re:人生苦短,我用Python(目录)满满的干货啊--Just-Like3. Re:flask-session组件filesystem跟mogodb中能够的注释没修改还是redis--拓荒牛wr4. Re:Python学习【第2篇】:Python数据结构支持你 呦--智超5. Re:数据库之 表与表之间的关系没找到想要的,但是还是收益的。回顾了以前知识--北风唐

阅读排行榜

1. 人生苦短,我用Python(目录)(23063)2. 数据库之 表与表之间的关系(11003)3. 函数和方法的区别(7124)4. Django框架之第三篇模板语法(重要!!!)(6415)5. NumPy:数组计算(4138)

评论排行榜

1. 人生苦短,我用Python(目录)(41)2. kail linux 系统下利用metaspolit工具渗透win7电脑(11)3. 数字类型和字符串类型(7)4. python-------装饰器(4)5. python并发编程之进程池,线程池,协程(4)

推荐排行榜

1. 人生苦短,我用Python(目录)(21)2. Django框架之第三篇模板语法(重要!!!)(3)3. restful framework 认证源码流程(3)4. BeatifulSoup模块(3)5. map函数和reduce函数的区别(3)Copyright ©2018 海燕。

python数据类型

评论关闭