-
输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)
例如: 输入**'abcd1234 ’ ** 输出**‘bd24’**
str1 = 'abcd1234' for i in range(len(str1)): if i % 2 != 0: print(str1[i])
-
输入用户名,判断用户名是否合法(用户名长度6~10位)
name_str = input("请输入名字:") if not 6 <= len(name_str) <= 10: print("你输入的用户名不合法") else: print("你输入的用户名正确")
-
输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)
例如: ‘abc’ — 合法 ‘123’ — 合法 ‘abc123a’ — 合法
name_str = input("请输入用户名:") for s in name_str: tem = ord(s) if not (48 <= tem <= 57 or 65 <= tem <= 90 or 97 <= tem <= 122): print("你输入的用户名不能是除了字母和数字的其他字符") break
-
输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)
例如: ‘abc’ — 不合法 ‘123’ — 不合法 ‘abc123’ — 不合法 ‘Abc123ahs’ — 合法 ‘Abc’ — 不合法
name_str = input("请输入用户名:") for s in name_str: tem = ord(s) if not 65 <= ord(name_str[0]) <= 90: print("你输入的用户名首字母应该大写") break if not (48 <= tem <= 57 or 65 <= tem <= 90 or 97 <= tem <= 122): print("你输入的用户名不能是除了字母和数字的其他字符") break
-
输入一个字符串,将字符串中所有的大写字母取出来产生一个新的字符串
例如:输入**‘abFc1shj2A3klBs99+2kDkk’** 输出:‘FABD’
str1 = input("请输入字符串:") new_str = "" for s in str1: tem = ord(s) if 65 <= tem <= 90: new_str += s print(new_str)
-
输入字符串,将字符串的开头和结尾变成’+',产生一个新的字符串
例如: 输入字符串**‘abc123’, 输出’+bc12+'**
str2 = input("请输入一个字符串:") new_str2 = "+" for s in range(1, len(str2) - 1): new_str2 += str2[s] new_str2 += "+" print(new_str2)
-
输入字符串,获取字符串的中间字符
例如: 输入**‘abc1234’** 输出:‘1’ 输入**‘abc123’** 输出**‘c1’**
str3 = input("请输入一个字符串:") length = len(str3) mid_len = int(length / 2) mid_str = '' if len(str3) != 0: if length % 2 != 0: print(mid_str + str3[mid_len]) else: print(mid_str + str3[mid_len - 1] + str3[mid_len]) else: print("输入的字符串为空")
-
(难)写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置)
例如: 字符串1为:how are you? Im fine, Thank you! , 字符串2为:you, 打印8
-
获取两个字符串中公共的字符
例如: 字符串1为:abcaaa123, 字符串2为: huak3 , 打印:公共字符有:a3
str1 = 'abcaaa123' str2 = 'huak3' str3 = '' for i in str1: for j in str2: if i == j: if i not in str3: str3 += i print("公共字符有:", str3)
-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
students = [] while 1: hands = int(input("1.添加学生" "2.查询学生" "3.退出" "请输入操作:")) if 1 == hands: students.append({'name': input("请输入学生姓名:"), 'age': int(input("请输入学生年龄:")), 'score': float(input("请输入学生成绩:")), 'tel': input("请输入学生电话: "), 'gender': input("请输入学生性别:")}) elif hands == 2: print(students) elif hands == 3: break
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students = [{'name': '张三', 'age': 18, 'score': 89.3, 'tel': '13584696532', 'gender': '男'}, {'name': '赵四', 'age': 22, 'score': 98.3, 'tel': '16589653285', 'gender': '女'}, {'name': '王五', 'age': 16, 'score': 23.5, 'tel': '19656546552', 'gender': '女'}, {'name': '赵六', 'age': 40, 'score': 65.0, 'tel': '18489653578', 'gender': ''}, {'name': '孙七', 'age': 25, 'score': 89.0, 'tel': '18496535689', 'gender': ''}, {'name': '刘八', 'age': 18, 'score': 99.0, 'tel': '18654963256', 'gender': '男'}]
-
统计不及格学生的个数
count = 0 for stu in students: if stu['score'] < 60: count += 1 print(count)
-
打印不及格未成年学生的名字和对应的成绩
for stu in students: if stu['score'] < 60 and stu['age'] < 18: print("学生名字:", stu['name'], "成绩", stu['score'])
-
求所有男生的平均年龄
age_all = 0 count_man = 0 for stu in students: if stu['gender'] == '男': count_man += 1 age_all += stu['age'] print(age_all / count_man)
-
打印手机尾号是8的学生的名字
for stu in students: if stu['tel'][-1] == '8': print(stu['name'])
-
打印最高分和对应的学生的名字
max_score = students[0]['score'] index = 0 max_index = 0 for stu in students: if stu['score'] > max_score: max_score = stu['score'] max_index = index index += 1 print(students[max_index]['name'])
-
删除性别不明的所有学生
del_index = [] for i in range(len(students)): if students[i]['gender'] == "": del_index.append(i) for i in del_index: del students[i] print(students)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
for i in range(len(students) - 1): for j in range(i, len(students)): if students[i]['score'] < students[j]['score']: temp = students[i] students[i] = students[j] students[j] = temp print(students)
-