zoukankan      html  css  js  c++  java
  • 练习 : 数据类型之字符串

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

    例如: 输入'abcd1234' 输出 'bd24'
    str1 = input('请输入字符串:')
    for index1 in range(len(str1)):
        if index1 % 2 == 1:
            print(str1[index1])
    

    2.输入用户名,判断用户名是否合法(用户名长度6~10位)

    name1 = input('请输入用户名:')
    while True:
        if 6 <= len(name1) <= 10:
            print('用户名合法,欢迎!')
            break
        else:
            print('用户名不合法')
        name1 = input('请输入用户名:')
    

    3.输入用户名,判断用户名是否合法(用户名中只能由数字和字母组成)

    例如: 'abc' — 合法 '123' — 合法 ‘abc123a’ — 合法
    '''方法①'''
    name2 = input('请输入用户名:')
    for name_2 in name2:
        if not ('0' <= name_2 <= '9' or 'A' <= name_2 <= 'Z' or 'a' <= name_2 <= 'z'):
            print('不合法')
            break
    else:
        print('合法')
    
    '''方法②'''
    name2 = input('请输入用户名:')
    for name_2 in name2:
        if name_2.isalnum() == False:
            print('不合法')
            break
    else:
        print('合法')
    

    4.输入用户名,判断用户名是否合法(用户名必须包含且只能包含数字和字母,并且第一个字符必须是大写字母)

    例如: 'abc' — 不合法 '123' — 不合法 'abc123' — 不合法 'Abc123ahs' — 合法
    name3 = input('请输入用户名:')
    for name_3 in name3:
        if not 'A' <= name3[0] <= 'Z':
            print('不合法')
            break
        elif not ('0' <= name_3 <= '9' or 'A' <= name_3 <= 'Z' or 'a' <= name_3 <= 'z'):
            print('不合法')
            break
        elif name3.isalpha() == True or name3.isdigit() == True:
            print('不合法')
            break
    else:
        print('合法')
    

    5.# 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串

    例如:输入'abc1shj23kls99+2kkk' 输出:'123992'
    str2 = input('请输入:')
    list1 = []
    for value1 in str2:
        if '0' <= value1 <= '9':
            list1.append(value1)
    print(list1)
    print(''.join(list1))
    

    6.输入一个字符串,将字符串中所有的小写字母变成对应的大写字母输出(用upper方法和自己写算法两种方式实现)

    例如: 输入 'a2h2klm12+' 输出 'A2H2KLM12+'
    str3 = input('请输入:')
    print(str3.upper())
    list2 = []
    for value2 in str3:
        if 'a' <= value2 <= 'z':
            list2.append(chr(ord(value2) - 32))
            continue
        list2.append(value2)
    print(''.join(list2))
    

    7.输入一个小于1000的数字,产生对应的学号

    例如: 输入 '23',输出 'py1901023'输入 '9', 输出 'py1901009' 输入 '123',输出 'py1901123'
    num1 = input('请输入数字:')
    print('py1906', num1.rjust(3, '0'), sep='')
    print('py1906', num1.zfill(3), sep='')
    

    8. 输入一个字符串,统计字符串中非数字字母的字符的个数

    例如: 输入 'anc2+93-sj胡说' 输出: 4 输入 '===' 输出: 3
    str3 = input('请输入:')
    count1 = 0
    for oth in str3:
        if not ('0' <= oth <='9' or 'A' <= oth <= 'Z' or 'a' <= oth <= 'z'):
            count1 +=1
    print(count1)
    

    9. 输入字符串,将字符串的开头和结尾变成 '+',产生一个新的字符串

    例如: 输入字符串 'abc123', 输出 '+bc12+'
    str4 = input('请输入:')
    str5 = str4.replace(str4[0], '+')
    print(str5.replace(str5[-1], '+'))
    

    10. 输入字符串,获取字符串的中间字符

    例如: 输入 'abc1234' 输出: '1' 输入 'abc123' 输出 'c1'
    str6 = input('请输入:')
    if int(len(str6)) & 1 == 1:
        print(str6[int(len(str6)//2)])
    elif int(len(str6)) & 1 == 0:
        print(str6[int(len(str6) // 2 - 1)], str6[int(len(str6) // 2)])
    

    11. 写程序实现字符串函数find / index的功能(获取字符串1中字符串2第一次出现的位置)

    例如: 字符串1为:how are you? Im fine, Thank you!, 字符串2为: you, 打印8
    str7 = 'how are you? Im fine, Thank you!'
    str8 = 'you'
    for index7 in range(len(str7)):
        n = 0
        for index8 in range(len(str8)):
            if str7[index7] == str8[0]:
                if str7[index7 + 1] == str8[1]:
                    print(index7)
                    n += 1
                    break
        if n == 1:
            break
    

    12. 获取两个字符串中公共的字符

    例如: 字符串1为:abc123, 字符串2为: huak3, 打印: 公共字符有:a3
    str9 = 'abc123'
    str10 = 'huak3'
    set1 = set()
    for value9 in str9:
        for value10 in str10:
            if value9 == value10:
                set1.add(value9)
    print('公共字符有:',''.join(set1))
    
  • 相关阅读:
    处理emacs-org模式TODO的一个脚本
    MYSQL 数据类型
    Redis命令学习-Transaction(事务)
    成都青羊考场科目二考试分享
    地图入门_坐标系统
    microsoft SQL server,错误2
    搭建个人博客 方式2 使用jekyll
    WIN10 10招
    java正則表達式总结
    图解hdu5301Buildings
  • 原文地址:https://www.cnblogs.com/anjhon/p/11892622.html
Copyright © 2011-2022 走看看