num1,num2 = input('请输入字符串:'),input('请输入要查询的子串:') a = len(num1) b = len(num2) cd = 0 for i in range(a - 1): # 每次取和子字符串长度相等的字符串和子字符串进行比较 if num1[i:i + b] == num2: cd += 1 print("子串在字符串中出现的次数:%d" % cd)