在 Python 中,可以使用 input() 函数从键盘输入字符串。例如:
string = input('请输入字符串: ')
然后,可以使用字典来统计字符串中数字出现的次数。首先,可以创建一个空字典,然后遍历字符串中的每一个字符,如果这个字符是数字,就将它作为字典的键,并将其值加 1。
count = {}
for char in string:
if char.isdigit():
if char in count:
count[char] += 1
else:
count[char] = 1
最后,可以使用循环来遍历字典并输出结果。
for key, value in count.items():
print(f'数字 {key} 出现了 {value} 次')
完整的代码如下:
string = input('请输入字符串: ')
count = {}
for char in string:
if char.isdigit():
if char in count:
count[char] += 1
else:
count[char] = 1
for key, value in count.items():
print(f'数字 {key} 出现了 {value} 次')