zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python 用for 循环计算字符串中某个字符出现的次数

2023-04-18 14:23:23 时间

 str = 'a-bc-12----3'
num = []
for n in str:
    if n == '-':
        num += n
        continue
print(num.count('-'))

#或者
str = 'a-bc-12----3'
i = 0
for n in str:
    if n == '-':
        i += 1
        continue
print(i)