

a = '000itcast and itheima000'
import re
# 使用正则迭代,获取所有匹配,并将匹配结果中的位置信息放入到列表中
b = [n.span() for n in re.finditer('it',a,re.I)]
# 将位置信息打印出来
for i in range(len(b)):
print('第'+str(i+1)+'次位置:',b[i])
# 将位置信息列表长度打印出来
print('总计出现次数:',len(b))
# 使用正则替换指定字符
c = re.sub('000',' ',a)
print('替换000为空格:',c)
# 字符串转大写
d = c.upper()
print('字母转大写:',d)
# 字符串去头尾空格
e=d.strip()
print('去头尾空格:',e)
#1
def find_subs(s,sub):
r=[]
n=len(sub)
x=0
while True:
if s.find(sub)==-1:
break
i=s.find(sub)
r.append((i+x,i+x+n))
s=s[i+n:]
x+=i+n
return r
s='000itcast and itheima000'
a='it'
print(find_subs(s,a))
s=s.replace('000',' ')
print(s)
s=s.upper()
print(s)
s=s.strip()
print(s)
#2
week='三'
temp=33.6
cond='晴'
print('星期%s,温度%.1f,天气%s'%('三',33.6,'晴'))
print('星期{0:s},温度{1:.1f},天气{2:s}'.format('三',33.6,'晴'))
print(f'星期{week},温度{temp:.1f},天气{cond}')