#coding=utf-8 """ =========================== Author:多测师_王sir Time:2020-07-14 15:57 Wechat:xiaoshubass website:www.duoceshi.cn =========================== """ #企业笔试代码题: 有一个字符串X,求字符串X中字母a的位置和数量?? str = "abcabcaaduoceshi" list1 = [] num = 0 while str: i = str.find("a", num) if not i == -1: num = i + 1 list1.append(i) else: break print 'a的索引为{0[0]} {0[1]} {0[2]} {0[3]}'.format(list1) print 'a出现的次数为{}次'.format(len(list1))
答案如下:
a的索引为0 3 6 7
a出现的次数为4次