day9 - string job

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

    For example: input * *'abcd1234 '* * output * *'bd24'**

    str0 = 'abcd1234 '
    str1 =''
    for index in range(len(str0)):
    	if index % 2 !=0:
    		str1 += str0[index]
    print(str1)
    
  2. Enter the user name and judge whether the user name is legal (the length of the user name is 6 ~ 10 characters)

    name = input('Please enter user name:')
    count = 0
    for x in name:
        count += 1
    if not 6<=count <=10:
        print('Illegal name')
        name = input('Please enter user name:')
    else:
        print('The user name is legal')
    
    
  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

    name1 = input('Please enter user name:')
    for x in name1:
        if not('a'<=x<='z' or 'A'<=x<='Z' or '0'<=x<='9'):
            end1 = 'Illegal user name'
            print(end1)
            name1 = input('Please enter user name:')
            continue
    else:
        end1 = 'The user name is legal'
        print(end1)
    
  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 '123' - illegal 'abc123' - illegal 'Abc123ahs' - legal* (no)

    name2 = input('Please enter user name:')
    if 'A' <= name2[0] <= 'Z':
        for x in name2:
            if not('a'<=x<='z' or 'A'<=x<='Z' or '0'<=x<='9'):
                print('Illegal user name')
                name1 = input('Please enter user name:')
                continue
        else:
            print('The user name is legal')
    else:
        print('Illegal user name')
        name1 = input('Please enter user name:')
    
  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'

    str1 = 'abc1shj23kls99+2kkk'
    str2 = ''
    for x in str1:
        if '0' <= x <= '9':
            str2 += x
    print(str2)
    
  6. Input a string and change all lowercase letters in the string into corresponding uppercase letters for output (implemented by upper method and self writing algorithm)

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

    str3 = 'a2h2klm12+'
    #Method 1:
    str4 = str3.upper()
    print(str4)
    #Method 2:
    str5 = ''
    str3 = 'a2h2klm12+'
    for x in str3:
        if 'a' <= x <= 'z':
            a = ord(x) - 32
            x = chr(a)
        str5 += x
    print(str5)
    
  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 '**

    str6 = 'py1901'
    str7 = ''
    from random import randint
    num = randint(1,1000)
    if num // 10 == 0:
        str7 = str6 + '0' + '0' + str(num)
    elif 10 > num // 10 > 0:
        str7 = str6 + '0' + str(num)
    else:
        str7 = str6 + str(num)
    print(str7)
    
  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

    count = 0
    str8 = 'anc2+93-sj nonsense'
    for x in str8:
        if x < '0' or x >'9':
            count += 1
    print(count)
    
  9. Enter a string, change the beginning and end of the string to '+' to produce a new string

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

    str9 = 'abc123'
    new_str9 = '+' + str9[1:-1] + '+'
    print(new_str9)
    
  10. Enter a string to get the middle character of the string

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

str10 = 'abc1234'
lenth = len(str10)
if lenth % 2:
    print(str10[lenth // 2])
else:
    print(str10[lenth // 2 - 1],str10[lenth // 2 + 1])
  1. The writer implements the function of the string function find/index (get the position of 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 = 'how are you? Im fine, Thank you!'
str2 = 'you'
if str2 in str1:
    for index in range(len(str1)):
        if str1[index] == str2[0]:
            print('character string str2 The location of occurrence is:',index)
  1. Gets the common characters in two strings

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

str1 ='abc123'
str2 = 'huak3'
str3 = ''
for x in str1:
    for i in str2:
        if x == i:
            str3 += i
print('Common characters are:',str3)

‚Äč

Keywords: Python

Added by bealers on Sun, 26 Sep 2021 14:14:00 +0300