day5-字符串与字典

  1. 输入一个字符串,打印所有奇数位上的字符(下标是1,3,5,7…位上的字符)

例如: 输入**'abcd1234 ' ** 输出**'bd24'**

str1 = 'abcd1234'
new_str = ''
for x in range(1, len(str1), 2):
    new_str += str1[x]
print(new_str)
  1. 输入用户名,判断用户名是否合法(用户名长度6~10位)

account = input('请输入用户名:')
if 6 <= len(account) <= 10:
    print('用户名合法')
else:
    print('用户名非法')
  1. 输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)

例如: 'abc' — 合法 '123' — 合法 ‘abc123a’ — 合法

account = input('请输入用户名:')
for x in account:
    if x < '0' or '9' < x < 'A' or 'Z' < x < 'a' or x > 'z':
        print('不合法')
        break
else:
    print('合法')
  1. 输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)

例如: 'abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法 'Abc' — 不合法

account = input('请输入用户名:')
for x in account:
    if 'A' <= account[0] <= 'Z':
        if x < '0' or '9' < x < 'A' or 'Z' < x < 'a' or x > 'z':
            print('不合法')
            break
    else:
        print('不合法')
        break
else:
    print('合法')
  1. 输入一个字符串,将字符串中所有的大写字母取出来产生一个新的字符串

例如:输入**'abFc1shj2A3klBs99+2kDkk'** 输出:**'FABD'**

str1 = 'abFc1shj2A3klBs99+2kDkk'
new_str = ''
for x in str1:
    if 'A' <= x <='Z':
        new_str += x
print(new_str)
  1. 输入字符串,将字符串的开头和结尾变成'+',产生一个新的字符串

例如: 输入字符串**'abc123', 输出'+bc12+'**

str1 = input('请输入:')
new_str = ''
for x in range(1, len(str1)-1):
    new_str += str1[x]
new_str = '+' + new_str + '+'
print(new_str)
  1. 输入字符串,获取字符串的中间字符

例如: 输入**'abc1234'** 输出:'1' 输入**'abc123'** 输出**'c1'**

str1 = input('请输入:')
new_str = ''
if len(str1) % 2 == 0:
    new_str = str1[len(str1)//2 - 1] + str1[len(str1)//2]
else:
    new_str = str1[len(str1)//2]
print(new_str)
  1. (难)写程序实现字符串函数find/index的功能(获取字符串1中字符串2第一次出现的位置)

例如: 字符串1为:how are you? Im fine, Thank you! , 字符串2为:you, 打印8

str1 = input('请输入:')
str2 = input('请输入:')
count = 0
if str2 in str1:
    for x in range(len(str1)):
        if count != len(str2) - 1:
            if str1[x] == str2[0]:
                for y in range(1, len(str2)):
                    if str1[x+y] == str2[y]:
                        count += 1
                        if count == len(str2) - 1:
                            print(x)
                            break
                    else:
                        count = 0
                        break
        else:
            break
else:
    print('str1中不包含str2')
  1. 获取两个字符串中公共的字符

例如: 字符串1为:abcaaa123, 字符串2为: huak3 , 打印:公共字符有:a3

str1 = input('请输入:')
str2 = input('请输入:')
common_str = ''
for x in str1:
    if x in str2 and x not in common_str:
        common_str += x
print(common_str)
  1. 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别

dict1 = {'name': 'mary', 'age': 18, 'score': 99, 'tel': '123456789', 'gender': 'man'}
print(dict1)
  1. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )

  1. 统计不及格学生的个数

  1. 打印不及格未成年学生的名字和对应的成绩

  1. 求所有男生的平均年龄

  1. 打印手机尾号是8的学生的名字

  1. 打印最高分和对应的学生的名字

  1. 删除性别不明的所有学生

  1. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

student = [
    {'name': 'Mary', 'age': 18, 'score': 99, 'tel': '135798264', 'gender': 'man'},
    {'name': 'Joe', 'age': 26, 'score': 81, 'tel': '136469858', 'gender': 'woman'},
    {'name': 'Bob', 'age': 10, 'score': 46, 'tel': '136542588', 'gender': 'unknown'},
    {'name': 'Jan', 'age': 9, 'score': 59, 'tel': '136523651', 'gender': 'man'},
    {'name': 'Janny', 'age': 35, 'score': 99, 'tel': '562589655', 'gender': 'woman'},
    {'name': 'James', 'age': 17, 'score': 65, 'tel': '135856656', 'gender': 'man'}
]

# 统计不及格学生的个数
count = 0
for x in student:
    if x['score'] < 60:
        count += 1
print(count)

# 打印不及格未成年学生的名字和对应的成绩
for x in student:
    if x['score'] < 60 and x['age'] < 18:
        print(x['name'], x['score'])

# 求所有男生的平均年龄
mean_age = 0
for x in student:
    mean_age += x['age'] / len(student)
print(mean_age)

# 打印手机尾号是8的学生的名字
for x in student:
    if x['tel'][-1] == '8':
        print(x['name'])

# 打印最高分和对应的学生的名字
highest_score = max(student, key=lambda x: x['score'])
for y in student:
    if y['score'] == highest_score['score']:
        print(y['name'], y['score'])

# 将列表按学生成绩从大到小排序
new_student = sorted(student,key=lambda x: x['score'], reverse = True)
print(new_student)

# 删除性别不明的所有学生
for x in student:
    if x['gender'] == 'unknown':
        student.remove(x)
print(student)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值