Python程序输入一个字符串并查找总数的大写和小写字母
生活随笔
收集整理的這篇文章主要介紹了
Python程序输入一个字符串并查找总数的大写和小写字母
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Given a string str1 and we have to count the total numbers of uppercase and lowercase letters.
給定字符串str1 ,我們必須計算大寫和小寫字母的總數(shù)。
Example:
例:
Input: "Hello World!"Output:Uppercase letters: 2Lowercase letters: 8Input: "[email?protected]"Output:Uppercase letters: 1Lowercase letters: 4Method 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,0for c in str1:if c>='A' and c<='Z':no_of_ucase += 1if c>='a' and c<='z':no_of_lcase += 1print("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: 8RUN 2: nput a string: [email?protected] Input string is: [email?protected] Total number of uppercase letters: 1 Total number of lowercase letters: 4Method 2:
方法2:
By using islower() and isupper() methods
通過使用islower()和isupper()方法
print("Input a string: ") str1 = input()no_of_ucase, no_of_lcase = 0,0for 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: 8RUN 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
總結(jié)
以上是生活随笔為你收集整理的Python程序输入一个字符串并查找总数的大写和小写字母的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php无限极,php实现无限极分类 -
- 下一篇: Python operator.trut