|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
零基础学习python19课练习题
def count(*param):
length = len(param)
for i in range(length):
letters = 0
space = 0
digit = 0
other = 0
for each in param[i]:
if each.isalpha():
letters += 1
elif each.isdigit():
digit += 1
elif each == " ":
space += 1
else:
other += 1
print("第%d个字符串有:英文字母%d个,空格%d个,数字%d个,其他字符%d个"%(i+1,letters,space,digit,other))
count("汉字")
对于unicode string,string.isalpha会根据字符串中的字符是否属于Unicode编码的LETTER区域来判断是否都由字母组成。所以得出的结果为True,不一定表示只有26个英文字母。
- def count(*param):
- length = len(param)
- for i in range(length):
- letters = 0
- space = 0
- digit = 0
- other = 0
- for each in param[i]:
- print(each)
- if each.encode('UTF-8').isalpha():
- letters += 1
- elif each.encode('UTF-8').isdigit():
- digit += 1
- elif each == " ":
- space += 1
- else:
- other += 1
- print("第%d个字符串有:英文字母%d个,空格%d个,数字%d个,其他字符%d个"%(i+1,letters,space,digit,other))
复制代码
|
|