day10 - string job 1

  1. Enter a string and print all odd-numbered characters (subscripts are characters in 1, 3, 5, 7...)

    For example: input 'abcd1234' output 'bd24'

    str1 = 'abcd1234'
    print(str1[1::2])
    
  2. Enter the user name to determine whether the user name is legal (the length of the user name is 6~10 digits)

    account = input('please enter user name:')
    if 6 <= len(account) <= 10:
        print('Username is legal')
    else:
        print('Illegal username')
    
  3. Enter the user name to determine whether the user name is legal (the user name can only be composed of numbers and letters)

    Example: 'abc' - valid '123' - valid 'abc123a' - valid

    # method 1
    account = input('please enter user name:')
    for index in account:
        if not ('A' <= index <= 'Z' or 'a' <= index <= 'z' or '1' <= index <= '9'):
            print('Illegal username')
            break
    else:
        print('Username is legal')
    
    # Method 2
    account = input('please enter user name:')
    for index in account:
        if not (index.isascii() and index.isalnum()):
      # if not (index.islower() or index.isupper() or index.isdigit()):
            print('Illegal username')
            break
    else:
        print('Username is legal')
    
  4. Input a string, extract all the numeric characters in the string to generate a new string

    For example: input **'abc1shj23kls99+2kkk'** output: '123992'

    str1 = input('please enter:')
    new_str = ''
    for index in str1:
        if index.isdigit():
            new_str += index
    print(new_str)
    
  5. Input a string, change all lowercase letters in the string into corresponding uppercase letters and output (using the upper method and writing the algorithm yourself)

    Example: input **‘a2h2klm12+’ ** output ‘A2H2KLM12+’

    # method 1
    str1 = input('please enter:')
    print(str1.upper())
    
    # Method 2
    str1 = input('please enter:')
    new_str = ''
    for index in str1:
        if 'a' <= index <= 'z':
            new_str += chr(ord(index)-32)
        else:
            new_str += index
    print(new_str)
    
  6. Enter a number less than 1000 to generate the corresponding student number

    For example: input **'23', output 'py1901023'** input **'9', output 'py1901009'** input **'123', output 'py1901123'**

    num = input('please enter:')
    stu_acc = 'py1901' + num
    print(stu_acc)
    
  7. Enter a string and count the number of non-alphanumeric characters in the string

    For example: input **'anc2+93-sj nonsense'** output: 4 input **'==='** output: 3

    str1 = input('please enter:')
    count = 0
    for index in str1:
        if not (index.isascii() and index.isalnum()):
            count += 1
    print(count)
    
  8. Enter a string, change the beginning and end of the string to '+', and generate a new string

    For example: input string **'abc123', output '+bc12+'**

    str1 = input('please enter:')
    new_str = str1.replace(str1[0], '+')
    new_str = new_str.replace(str1[-1], '+')
    print(new_str)
    
  9. Enter a string and get the middle character of the string

    For example: input **'abc1234'** output: '1' input **'abc123'** output **'c1'**

    str1 = input('please enter:')
    length = len(str1)
    if length % 2:
        print(str1[int(length // 2)])
    else:
        print(str1[int(length // 2)], str1[int(length // 2 - 1)])
    
  10. Write a program to implement the function of the string function find/index (get the first occurrence of string 2 in string 1)

    For example: String 1 is: how are you? Im fine, Thank you! , String 2 is: you, print 8

    str1 = input('please enter:')
    str2 = input('please enter:')
    index = 0
    while True:
        if str1[index:index+3] == str2:
            print(index)
            break
        else:
            index += 1
        if index > len(str1)-3:
            print('String 1 does not have string 2')
            break
    
  11. Get the common characters in two strings

    For example: string 1 is: abc123, string 2 is: huak3, print: common characters are: a3

    str1 = input('please enter:')
    str2 = input('please enter:')
    if len(str1) > len(str2):
        str1, str2 = str2, str1
    new_str = ''
    for item in str1:
        if item in str2:
            new_str += item
    if new_str == '':
        print('no common characters')
    else:
        print(new_str)
    
    
  12. Enter the user name to determine whether the user name is legal (the user name must contain and can only contain numbers and letters, and the first character must be an uppercase letter)

    Example: 'abc' - illegal 'Mabc' - illegal '123' - illegal 'abc123' - illegal 'Abc123ahs' - legal

    account = input('please enter:')
    if account[0].isupper():
        if account.isascii() and account.isalnum():
            for item in account:
                if item.isdigit():
                    print('legitimate')
                    break
            else:
                print('illegal')
        else:
            print('illegal')
    else:
        print('illegal')
    

‚Äč

Posted by UKlee on Sun, 19 Mar 2023 03:11:35 +0530