计算字符串中有多少个整数

  • 先计算用户输入内容中有多少个整数(以个位数为单位)
content = input('请输入一段内容:').strip()  # 对输入的内容进行去空格操作  虽然没啥必要
count = 0  # 使用count来对出现次数计数
for i in content:  # 对字符串进行遍历
    if i.isdigit():  # 判断字符串是否为数字
        count += 1
print(f'{content}中出现了{count}次整数')
  • 接下来判断大写字母,小写字母,数字出现的次数(以个位数为单位)
content = input('请输入一句话:')
upper_alph = 0
lower_alph = 0
digit = 0
other = 0

for c in content:  # 循环输出字符串中的内容
    if c.isupper():  # 判断是否是大写字母
        upper_alph += 1
    elif c.islower():  # 判断是否是小写字母
        lower_alph += 1
    elif c.isdigit():  # 判断是否是数字
        digit += 1
    else:  # 其他字符
        other += 1
print(f'大写字母共出现了{upper_alph}次,小写字母共出现了{lower_alph}次,数字共出现了{digit}次,其他字符共出现了{other}次。')

猜你喜欢

转载自www.cnblogs.com/lcfzh/p/10051329.html
今日推荐