用 python 从键盘任意输入2个字符串分别存放到变量a、b中要求统计b变量中的字符串在a变量中出现的次数及对应的所有位置
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)
原文地址: https://www.cveoy.top/t/topic/cisw 著作权归作者所有。请勿转载和采集!