【发布时间】:2022-08-19 15:14:20
【问题描述】:
我有一个较长的字符串,其中包含多个位置的字符 \'#\',我想使用循环获取索引。
string = \'#test#\'
for index, i in enumerate(string):
print(\"Current position of # is \", index)
结果是:
Current position of # is 0
Current position of # is 1
Current position of # is 2
Current position of # is 3
Current position of # is 4
Current position of # is 5
我如何才能添加只有在字符串中找到 \'#\' 时才会出现输出?
我试过if \'#\' in string[i]: 导致错误string indices must be integers
有没有办法在不改变字符串类型的情况下做到这一点?
-
if i == \'#\'…?! -
i是特点,索引为...index。
标签: python