Python输入一个字符串,统计并输出其中数字的数量
时间: 2023-05-11 18:05:03 浏览: 152
可以使用 Python 的 isdigit() 函数来判断字符串中的字符是否为数字,然后统计数量即可。以下是示例代码:
```
s = input("请输入一个字符串:")
count = 0
for c in s:
if c.isdigit():
count += 1
print("字符串中数字的数量为:", count)
```
输入一个字符串后,程序会输出其中数字的数量。
相关问题
输入一个字符串,统计其中空格、数字、字母和其它字符的数量
好的,以下是 Python 代码实现:
```python
string = input("请输入字符串:")
space_count = 0
digit_count = 0
letter_count = 0
other_count = 0
for char in string:
if char.isspace():
space_count += 1
elif char.isdigit():
digit_count += 1
elif char.isalpha():
letter_count += 1
else:
other_count += 1
print("空格数量:", space_count)
print("数字数量:", digit_count)
print("字母数量:", letter_count)
print("其它字符数量:", other_count)
```
输入一个字符串后,程序会输出该字符串中空格、数字、字母和其它字符的数量。
python输入一个字符串,分别计算其中的小写字符,大写字符,数字,其它字符的个数
好的,我知道了。可以使用Python的内置函数islower(), isupper(), isdigit()来判断字符的类型,然后统计数量。以下是实现代码:
```python
def count_str(s):
n_upper = n_lower = n_digit = n_other = 0
for c in s:
if c.islower():
n_lower += 1
elif c.isupper():
n_upper += 1
elif c.isdigit():
n_digit += 1
else:
n_other += 1
return n_lower, n_upper, n_digit, n_other
s = input('请输入一个字符串:')
n_lower, n_upper, n_digit, n_other = count_str(s)
print("小写字母个数:", n_lower)
print("大写字母个数:", n_upper)
print("数字个数:", n_digit)
print("其它字符个数:", n_other)
```
输入一个字符串后,即可输出其中各种类型字符出现的个数。