[转载] Python程序输入一个字符串并查找总数的大写和小写字母

参考链接: Python程序可大写字符串中每个单词的第一个和最后一个字符

Given a string str1 and we have to count the total numbers of uppercase and lowercase letters. 

  给定字符串str1 ,我们必须计算大写和小写字母的总数。  

 Example: 

  例:  

     Input: 

    "Hello World!"

    Output:

    Uppercase letters: 2

    Lowercase letters: 8

 

    Input: 

    "[email protected]"

    Output:

    Uppercase letters: 1

    Lowercase letters: 4

 

 

 Method 1: 

  方法1:  

 (Manual) By checking each character of the string with a range of uppercase and lowercase letters using the conditional statement. 

  (手动)通过使用条件语句检查字符串中每个字符的大小写范围。  

 print("Input a string: ")

str1 = input()

 

no_of_ucase, no_of_lcase = 0,0

 

for c in str1:

    if c>='A' and c<='Z':

        no_of_ucase += 1

    if c>='a' and c<='z':

        no_of_lcase += 1

 

print("Input string is: ", str1)

print("Total number of uppercase letters: ", no_of_ucase)

print("Total number of lowercase letters: ", no_of_lcase)

 

 

 Output 

  输出量  

 RUN 1:

Input a string: 

Hello World!

Input string is:  Hello World!

Total number of uppercase letters:  2

Total number of lowercase letters:  8

 

RUN 2:

nput a string: 

[email protected]

Input string is:  [email protected]

Total number of uppercase letters:  1

Total number of lowercase letters:  4

 

 

 Method 2: 

  方法2:  

 By using islower() and isupper() methods 

  通过使用islower()和isupper()方法  

 print("Input a string: ")

str1 = input()

 

no_of_ucase, no_of_lcase = 0,0

 

for c in str1:

  no_of_ucase += c.isupper()

  no_of_lcase += c.islower()

 

print("Input string is: ", str1)

print("Total number of uppercase letters: ", no_of_ucase)

print("Total number of lowercase letters: ", no_of_lcase)

 

 

 Output 

  输出量  

 RUN 1:

Input a string: 

Hello World!

Input string is:  Hello World!

Total number of uppercase letters:  2

Total number of lowercase letters:  8

 

RUN 2:

nput a string: 

[email protected]

Input string is:  [email protected]

Total number of uppercase letters:  1

Total number of lowercase letters:  4

 

 

 

 

  翻译自: https://www.includehelp.com/python/program-to-input-a-string-and-find-total-number-of- uppercase-and-lowercase-letters.aspx

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值