练习题:

1.验证码:0-9,a-zA-Z

def check_yzm():

str_yzm=''
for i in range(4):
num1 = random.randrange(0, 6)
if num1 == 1 or num1 == 3:
num2 = random.randrange(0, 10)
str_yzm += str(num2)

elif num1 == 2 or num1 == 4:
num3 = random.randrange(97, 123)
str2 = chr(num3)
str_yzm += str(str2)

else:
num4 = random.randrange(65, 91)
str1 = chr(num4)
str_yzm += str(str1)
return str_yzm

yzm = check_yzm()
print(yzm)


2 写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数

def count_str(str):
num_count = 0
letter_count = 0
space_count = 0
others_count = 0
for each_str in str :
if each_str.isdigit():
num_count += 1
elif each_str.isalpha():
letter_count += 1
elif each_str.isspace():
space_count += 1
else:
others_count += 1
return '数字个数为:{num_count},字母个位数为:{letter_count},空格个数为:{space_count},其他的个数为:{others_count}'.format(num_count=num_count,letter_count=letter_count,space_count= space_count,others_count=others_count)


str1 = "123 ***qwe"
result = count_str(str1)
print(result)
原文地址:https://www.cnblogs.com/lovuever/p/6603850.html