元祖变成字符串 || 字符串中替换部分字符串 || enumberate可以找到字符串,列表,元祖的的索引和值 || zip的用法 || 判断变量的类型

 元祖变成字符串采用这种方式:

 

 字符串中替换部分字符串

from string import Template

s = Template('hello,${name} how are you,this is ${point},welcome to you')
d = s.substitute(name = 'tom',point = 'beijing')
print d
输出:hello,tom how are you,this is beijing,welcome to you
#enumberate可以找到字符串,列表,元祖的的索引和值
f = 'helloworld' for i,t in enumerate(f): if(i == 3): print t 输出:索引3对应的值是: l a = ['aa','bb','cc','dd'] for i,t in enumerate(a): if(t == 'bb'): print 'bb的索引是:',i 输出:bb的索引是: 1 c = ('ee','ff','gg','hh') for i,t in enumerate(c): if('gg'==t): print 'gg的索引是:',i
输出:gg的索引是: 2

zip(arg0,arg1)

s ,t = 'hellotom','world'
print zip(s,t)
输出:[('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]

sss = ['aaa','bbb','ccc','ddd']
ppp = ['ggg','hhh','iii','kkk','jjj']
print zip(sss,ppp)

输出:[('aaa', 'ggg'), ('bbb', 'hhh'), ('ccc', 'iii'), ('ddd', 'kkk')]
 
ss = ('tt','yy','uu','ii')
pp = ('11','22','33','44','55','66')
print zip(pp,ss)
输出:[('11', 'tt'), ('22', 'yy'), ('33', 'uu'), ('44', 'ii')]

它只匹配对应的,成对输出,剩下的不输出

返回的都是固定格式的,返回元祖组成的列表,这是确定的格式。

数组,字符串,元祖他们相互之间是可以用zip()的:

s4 = ('acv','bdv','eger')
p4 = ['ag','sfd','asf','sf']
print zip(s4,p4)
输出:[('acv', 'ag'), ('bdv', 'sfd'), ('eger', 'asf')]

s5 = 'helloworld'
p5 = ['aa','bb','ee','ff']
print zip(s5,p5)
输出:[('h', 'aa'), ('e', 'bb'), ('l', 'ee'), ('l', 'ff')]

s6 = 'perfectOne'
p6 = ('eee','yyy','uuu','www','lll')
print zip(s6,p6)
输出:[('p', 'eee'), ('e', 'yyy'), ('r', 'uuu'), ('f', 'www'), ('e', 'lll')]

判断变量的类型:isinstance('abc',str)

print isinstance('abc',str)
输出:True
print type('abc')
输出:<type 'str'> python中的string是str
原文地址:https://www.cnblogs.com/lonely-buffoon/p/6279769.html