|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
def findStr(desStr, subStr):
count = 0
length = len(desStr)
if subStr not in desStr:
print('在目标字符串中未找到字符串!')
else:
for each1 in range(length-1):
if desStr[each1] == subStr[0]:
if desStr[each1+1] == subStr[1]:
count += 1
print('子字符串在目标字符串中共出现 %d 次' % count)
desStr = input('请输入目标字符串:')
subStr = input('请输入子字符串(两个字符):')
findStr(desStr, subStr)
第七行,为什么each要加个1?length为什么要加个“-1”?
去掉他们运行还会报错
length是为了带入索引值,即字符串下标,
-1是为了each1+1最多为length-1,
比如列表L=[1,2,3],长度3,索引值最大的元素是L[2]而不是L[3]
因为索引值从0开始数不是从1开始
|
|