如果字符串中的字符是特殊字符(例如#。@。$),是否有一個特定的函數返回true?比如,如果字符串中的所有字符都是字母,則isalpha()函數返回true。Python字符串處理函數
我必須創建一個程序,我需要向用戶詢問一個字符串,然後我的程序必須打印字符串的長度,字母數量,數字位數以及不是字母的字符數。
counter = 0
num = 0
extra = 0
wrd = raw_input("Please enter a short sentence.")
for i in wrd:
if i.isalpha():
counter = counter + 1
print "You have " + str(counter) +" letters in your sentence."
for n in wrd:
if n.isnumeric():
num = num + 1
print "You have " + str(num) + " number(s) in your sentence"
for l in wrd:
extra = extra + 1
print "You have " + str(extra) + " characters that are not letters or numbers."
我拿到了前兩個部分想通了,雖然我卡上的最後一個...我知道它的容易,只需創建一個while循環,但是,因爲我已經開始了,我要堅持三個四個環。
爲什麼不'len(wrd) - counter - num'? – TigerhawkT3