a = input('请输入字符串a:') b = input('请输入字符串b:') count = 0 positions = [] start = 0 while True: index = a.find(b, start) if index == -1: break count += 1 positions.append(index) start = index + 1 print('字符串b在字符串a中出现的次数为:', count) print('字符串b在字符串a中出现的位置为:', positions)

Python 字符串匹配:统计子字符串出现次数及位置

原文地址: https://www.cveoy.top/t/topic/nsK8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录