当我遇到这个时,我正在经历一些事情......
a = 'hello'
print(a.count('l'))
print(a.count(''))
输出是:
2
6
谁能解释一下为什么第二个输出是6?
最佳答案
count
此处返回子字符串出现的次数,此处为 ''
字符串。
hello 中每个字符之间确实有六个 ''
:
print(''.count('')) # >> 1
# You could symbolize it like that:
print('a'.count('')) # >> 2
# is equivalent to:
print('|a|'.count('|')) # >> 2
关于python - 什么都没有发生,甚至空间也没有发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54519117/