如何计算python [duplicate]中给定字符串的空间数

问题描述 投票:7回答:1
a=input("Enter the value  ")

for i in a:
  if a[i]==(" "):
       print("Space is at position", i)

我正在尝试计算给定字符串中的空格数,但是我的程序无法正常工作。

python
1个回答
8
投票
>>> a=input("Enter the value ")
Enter the value "My Testing String"
>>> a.count(' ')
2
© www.soinside.com 2019 - 2024. All rights reserved.