【练习题】python字符串练习1

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

    例如: 输入’abcd1234 ’ 输出’bd24’

    str1= 'abcd1234 '
    result =[x for x in str1[1::2]]
    print(result)
    #升级版2.0
    str1 = 'abcd1234 '
    print(str1[1::2])
    
  2. 输入用户名,判断用户名是否合法(用户名长度6~10位)

    user_name = input('请输入用户名:')
    if 6 <= len(user_name) <= 10:
        print("用户名合法")
    else:
        print("用户名不合法")
    #升级版2.0
    user_name = input('请输入用户名:')
    if 6 <= len(user_name) <= 10:
        print(f'{user_name}用户名合法')
    else:
        print(f'{user_name}用户名不合法')
    
  3. 输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)

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

    user_name = input('请输入用户名:')
    if not('0' <= user_name <= '9' or ('A' <= user_name <= 'Z' or 'a' <= user_name <= 'z')):
        print("用户名不合法")
    else:
        print("用户名合法")
    
    # 升级版2.0
    user_name = input('请输入用户名:')
    for x in user_name:
        if not(x.isdigit() or x.isupper() or x.islower()):
            print(f'{user_name}用户名不合法')
            break
    else:
        print(f'{user_name}用户名合法')
    
  4. 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串

    例如:输入**‘abc1shj23kls99+2kkk’** 输出:‘123992’

    str2 = 'abc1shj23kls99+2kkk'
    new_str2 = ''
    for x in str2:
        if '0' <= x <= '9':
            new_str2 += x
    print(new_str2)
    
    # 升级版2.0 ——把需要的字符串提取出来放在列表里面
    str2 = 'abc1shj23kls99+2kkk'
    new_str2 = ''.join([x for x in str2 if x.isdigit()])
    print(new_str2)
    
  5. 输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出 (用upper方法和自己写算法两种方式实现)

    例如: 输入**‘a2h2klm12+’ ** 输出 ‘A2H2KLM12+’

    # 字符串.upper()
    str3 = 'a2h2klm12+'
    print(str3.upper())
    # 方法2
    str3 = 'a2h2klm12+'
    new_str3 = ''
    for x in str3:
        if 'a' <= x <= 'z':
            new_str3 += chr(ord(x)-32)
        else:
            new_str3 += x
    print(new_str3)
    
  6. 输入一个小于1000的数字,产生对应的学号

    例如: 输入**‘23’,输出’py1901023’** 输入**‘9’, 输出’py1901009’** 输入**‘123’,输出’py1901123’**

    str1 = 'py1901000' 
    str2 = input('请输入小于1000的数:') 
    str3 = ''
    for x in range(len(str1) - len(str2)):
        str3 += str1[x]
    str3 += str2
    print(str3)
    
  7. 输入一个字符串,统计字符串中非数字字母的字符的个数

    例如: 输入**‘anc2+93-sj胡说’** 输出:4 输入**‘===’** 输出:3

    str4 = 'anc2+93-sj胡说'
    new_str4 = ''
    count = 0
    for x in str4:
        if not('0' <= x <= '9' or 'A' <= x <= 'Z' or 'a' <= x <= 'z'):
            new_str4 += x
            count += 1
    print(count)
    
  8. 输入字符串,将字符串的开头和结尾变成’+',产生一个新的字符串

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

    str1 = 'abc123'
    result = str1.replace('a', '+')
    result1 = result.replace('3', '+')
    print(result1)
    
    # 方法2.0
    print(f'+{str1[1:-1]}+')
    
  9. 输入字符串,获取字符串的中间字符

    例如: 输入**‘abc1234’** 输出:‘1’ 输入**‘abc123’** 输出**‘c1’**

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

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

    str1 = 'how are you? Im fine, Thank you!'
    str2 = 'you'
    l_str1 = len(str1)
    l_str2 = len(str2)
    # 0 ‘how’ ——> str1[0:3]
    # 1 "ow " ——> str1[1:4]
    # 2 'w a' ——> str1[2:5]
    #index  ——> str1[index:index+l_str2]
    
    for index in range(l_str1 - l_str2 + 1):
        if str2 == str1[index:index+l_str2]:
            print(index)
            break
    else:
        print("找不到!")
    
  11. 获取两个字符串中公共的字符

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

    str1 = 'abc123'
    str2 = 'abc456'
    count = 0
    new_str1 = []
    for x in str1:
        if x in str2 and x not in new_str1:
                new_str1 += x
                count += 1
    print('公共字符为:', new_str1)
    
    if count == 0:
        print('不存在公共字符')
        
    # 方法2.0 ——用集合超简单!!!
    str1 = 'abc123'
    str2 = 'abc456'
    result = "".join(set(str1) & set(str2))
    print(result)
    
  12. 输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)

    例如: ‘abc’ — 不合法 ‘Mabc’ — 不合法 ‘123’ — 不合法 ‘abc123’ — 不合法 ‘Abc123ahs’ — 合法

root = input("请输入用户名:")
new_root = ""
for x in root:
    if '0' <= x <= '9' or ('A' <= x <= 'Z' or "a" <= x <= "z"):
        new_root += x
        if 'A' <= new_root[0] <= 'Z':
            print("合法")
            break
else:
    print("不合法")
  

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值