New-Python-数据类型、字符编码、文件处理
一、字符串的一些应用:
#strip name='*egon**' print(name.strip('*')) print(name.lstrip('*')) print(name.rstrip('*')) #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)) #format的三种玩法 res='{} {} {}'.format('egon',18,'male') res='{1} {0} {1}'.format('egon',18,'male') res='{name} {age} {sex}'.format(sex='male',name='egon',age=18) #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(':')) #默认分隔符为空格 name='C:/a/b/c/d.txt' #只想拿到顶级目录 print(name.split('/',1)) name='a|b|c' print(name.rsplit('|',1)) #从右开始切分 #join tag=' ' print(tag.join(['egon','say','hello','world'])) #可迭代对象必须都是字符串 #center,ljust,rjust,zfill name='egon' print(name.center(30,'-')) print(name.ljust(30,'*')) print(name.rjust(30,'*')) print(name.zfill(50)) #用0填充 #expandtabs name='egon\thello' print(name) print(name.expandtabs(1)) #lower,upper name='egon' print(name.lower()) print(name.upper()) #captalize,swapcase,title print(name.capitalize()) #首字母大写 print(name.swapcase()) #大小写翻转 msg='egon say hi' print(msg.title()) #每个单词的首字母大写 #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()) print(num5.isdecimal()) print(num5.isnumeric()) ''' 总结: 最常用的是isdigit,可以判断bytes和unicode类型,这也是最常见的数字应用场景 如果要判断中文数字或罗马数字,则需要用到isnumeric ''' #is其他 print('===>') name='egon123' print(name.isalnum()) #字符串由字母或数字组成 print(name.isalpha()) #字符串只由字母组成 print(name.isidentifier()) print(name.islower()) print(name.isupper()) print(name.isspace()) print(name.istitle())
1 #请输出 name 变量对应的值的后 2 个字符? 2 #获取子序列,去掉最后一个字符。如: oldboy 则获取 oldbo。 3 name = " aleX" 4 5 print(name[-2:]) #空代表取到最后,或从最一开始取 6 print(name[:-1])
二、列表功能
my_girl_friends=['alex','wupeiqi','yuanhao',4,5] #本质my_girl_friends=list([...]) print(my_girl_friends[2]) print(my_girl_friends[-1]) my_girl_friends[5]='6号' #增加 print(id(my_girl_friends)) my_girl_friends[0]='SB' #更改 print(my_girl_friends) print(id(my_girl_friends)) #id不变 print(my_girl_friends[0:2]) #截取 print(my_girl_friends[0:2:1]) print(my_girl_friends[0:4]) print(my_girl_friends[0:4:2]) print(len(my_girl_friends)) print('alex' in my_girl_friends) print(5 in my_girl_friends) my_girl_friends.append('6号') del my_girl_friends[2] #通用删除法 print(my_girl_friends) my_girl_friends.remove('yuanhao') print(my_girl_friends) my_girl_friends.pop() #拿走一个值,而不是单纯的删除 my_girl_friends.pop() my_girl_friends.pop(1) #按照索引去删 print(my_girl_friends.pop(1)) print(my_girl_friends) res=my_girl_friends.pop(1) print(res) my_girl_friends=['alex','wupeiqi','yuanhao',4,'alex',5] #本质my_girl_friends=list([...]) my_girl_friends.insert(0,'sb_alex') print(my_girl_friends) my_girl_friends.insert(2,'yh') my_girl_friends.extend([0,1,2,3,4,5]) print(my_girl_friends) print(my_girl_friends.count('alex')) my_girl_friends.clear() print(my_girl_friends) l=my_girl_friends.copy() print(l) my_girl_friends.reverse() print(my_girl_friends) l=[1,2,6,4,-1,3] l.sort() l.sort(reverse=True) print(l) append,pop:队列,先进先出 l=[] l.append('first') l.append('second') l.append('third') print(l) print(l.pop(0)) print(l.pop(0)) print(l.pop(0)) insert,pop:堆栈,先进后出 l=[] l.insert(0,'first') l.insert(0,'second') l.insert(0,'third') print(l) print(l.pop(0)) print(l.pop(0)) print(l.pop(0)) l1=[] l1.insert(0,'first') l1.insert(0,'second') l1.insert(0,'third') print(l1) print(l1.pop(0)) print(l1.pop(0)) print(l1.pop(0))
作者:大雄猫
出处:http://www.cnblogs.com/guoxiangqian/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面
明显位M给出原文连接,否则保留追究法律责任的权利。