day11

day11
8.28 小练习
1 统计输入的字符多少个
>>> strs = str(input('pelase input str'))
pelase input strtiyiyubhnljikmltuyohij;451515
>>> num = 0
>>> for i in strs:
... num +=1
>>> num
29
  2 统计输入的字符多少个字母和数字
import string
str_leeters = string.ascii_letters
number = 0
str_num = 0
strs = str(input('pelase input str'))
for i in strs:
if i in str_letters:
str_num += 1
elif i in '0123456789':
num += 1
else:
print('error')
  3 将字符串中的数字去掉,只保留字母
s = 'fdsaf44523fgj23j3kj23'
strs = ''
for i in s:
if i not in '0123456789':
ss = i
strs = ss
  4 输入3个学生成绩,求和、平均数和最好成绩
n1 = int(input('please input sore'))
n2 = int(input('please input sore'))
n3 = int(input('please input sore'))
add = n1+n2+n3
avg =(n1+n2+n3)/3
n = []
n.append(n1)
n.append(n2)
n.append(n3)
good = n[0]
for i in n:
if i > good:
good = i
  5 输入一个学生成绩,判断是优良中及格和不及格
sore = int(input('please input sore'))
if sore < 60:
print("不及格")
elif sore >=60 and sore <80:
print("及格")
elif sore >=80 and sore <=100:
print("优良")
else:
print("你输入的是啥?")
  6 生成3个随机字母的字符串,生成一个随机小数的字符串 2个整数位和2个小数位
import string,random
str_leeters = string.ascii_letters
strs = ''
for i in range(3):
s = random.choice(str_letters)
strs += s
 
  7 统计一个数字中包含奇数数字的个数
num = 2524351412798
count = 0
for i in num:
if i % 2 ==1:
count += 1
  8 将数字的每一位+2,转换成一个新数字。(整数和小数)
整数
num = 32412
new_num = ''
for i in str(num):
n = int(i) +2
new_num += str(n)
int(new_num)
小数
num = 514.3412
new_num = ''
for i in str(num):
if i != '.':
n = int(i) +2
new_num += str(n)
int(new_num)
 
  9 将每个小写字母转换为大写字母,大写字母转换为小写字母
s =" AaBbCcDdEd"
ss = ''
import string
string_lowercase = string.ascii.lowercase
string_uppercase = string.ascii_uppercase
for i in s:
if i in string_lowercase:
s = chr(ord(i)-32) # s = i.upper()
ss += s
elif i in string_uppercase:
s = chr(order(i)+32) # s = i.lower()
ss += s
else:
print("请检查输入是否是字母")
 
  10 将一个bytes类型的gbk字符串,转为utf-8编码的bytes字符串
(用什么编码的就要用什么解码,然后再编码成想要的类型,gbk编码的就要先用gbk解码,再转utf-8)
>>> a = "中国"
>>> a.encode('gbk')
b = b'\xd6\xd0\xb9\xfa'
b.decode('gbk').encode('utf-8')
 
 
 
 

posted @ 2019-12-23 09:40  绝世老中医  阅读(221)  评论(0编辑  收藏  举报