day9 string job

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

    For example, input 'abcd1234' and output 'bd24'

    strs=input('Please enter a string:')
    c=''
    for i in range(1,len(strs),2):
        c+=strs[i]
    print(c)
    
  2. Enter the user name and judge whether the user name is legal (the length of the user name is 6 ~ 10 characters)

    names=input('enter one user name:')
    print(6<=len(names)<=10)
    
  3. Enter the user name to judge whether the user name is legal (the user name can only be composed of numbers and letters)

    For example, 'ABC' - Legal '123' - Legal 'abc123a' - Legal

    names=input('enter one user name:')
    for i in names:
        if not('0'<=i<='9' or  'a'<=i<='z' or  'A'<=i<='Z'):
            print(False)
            break
    else:
        print(True)
    
  4. Enter the user name and judge whether the user name is legal (the user name must contain and can only contain numbers and letters, and the first character must be capital letters)

    For example, 'ABC' - illegal 'Mabc' - illegal '123' - illegal 'abc123' - illegal 'Abc123ahs' - Legal

    names=input('enter one user name:')
    nums='0123456789'
    lists=list(nums)
    flag=False
    if not ('A'<=names[0]<='Z'):
        print(False)
    else:
        for i in names:
            if '0' <= i <= '9':
                flag=True
            if not ('0' <= i <= '9' or 'a' <= i <= 'z' or 'A' <= i <= 'Z'):
                print(False)
                break
    if flag:
        print(True)
    else:
        print(False)
    
  5. Enter a string and take out all the numeric characters in the string to produce a new string

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

    strs=input('Please enter a string:')
    strs1=''
    for i in strs:
        if '0' <= i <= '9':
            strs1+=i
    print(strs1)
    
  6. Input a string and output all lowercase letters in the string into corresponding uppercase letters (implemented by upper method and self writing algorithm)

    For example: input * * 'A2H2KLM12 +' * * output 'A2H2KLM12 +'

    strs=input('Please enter a string:')
    print(strs.upper())
    strs1=''
    for i in strs:
        if 'a' <= i <= 'z':
            strs1+=chr((ord(i)-32))
        else:
            strs1+=i
    print(strs1)
    
  7. 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'**

    strs=input('Please enter your student number:')
    st='py1901'
    num=len(strs)
    s=st+'0'*(3-num)+strs
    print(s)
    
  8. Enter a string to count the number of non alphanumeric characters in the string

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

    strs=input('Please enter a string:')
    sums=0
    for i in strs:
        if 'a' <= i <= 'z' or 'A' <= i <= 'Z':
            sums+=1
    print(sums)
    
  9. Enter a string, change the beginning and end of the string into '+' to produce a new string

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

    strs=input('Please enter a string:')
    s='+'+strs[1:-1]+'+'
    print(s)
    
  10. Enter a string to get the middle character of the string

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

    strs=input('Please enter a string:')
    num=len(strs)
    if num%2==0:
        print(strs[num//2-1:num//2+1])
    else:
        print(strs[num // 2 ])
    
  11. The writer realizes the function of string function find/index (get the position of string 2 in string 1 for the first time)

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

    str1=input('Please enter was_found character string:')
    str2=input('Please enter find character string:')
    num=len(str2)
    for i in range(len(str1)):
        if num>len(st1):
            print('find String length greater than was_found character string,Unable to find.')
        if str2 == str1[i:i+num]:
            print(i)
            break
    else:
        print(None)
    
  12. Gets the common characters in two strings

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

    a=input('Please enter string 1:')
    b=input('Please enter string 2:')
    c=''
    for i in a:
        if i in b and i not in c:
            c+=i
    print('The common character is',c)
    

Keywords: Python string

Added by PTS on Thu, 24 Feb 2022 14:47:03 +0200