day9 - string job

Keywords: Python

  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'**

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

    user = input('Please enter user name:')
    if 6 <= len(user) <= 10:
        print('Legal!')
    else:
        print('wrongful!')
    
  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

    str3 = input('Please enter user name:')
    for x in str3:
        if '\u4e00' <= x <= '\u9fa5':
            print('wrongful')
            break
    else:
        print('legitimate')
    
  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

    str4 = input('Please enter user name:')
    for x in str4:
        if not 'A' <= str4[0] <= 'Z' or (not '0' <= x <= '9' and not 'a' <= x <= 'z' and not 'A' <= x <= 'Z'):
            print('wrongful')
            break
    else:
        print('legitimate')
    
  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'

    str5 = input('Please enter a string:')
    new_str5 = ''
    for x in str5:
        if '0' <= x <= '9':
            new_str5 += x
    print(new_str5)
    
  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 +'

    # Method 1:
    str6 = input('Please enter a string:')
    print(str6.upper())
    
    # Method 2:
    str6 = list(input('Please enter a string:'))
    new_str6 = ''
    for x in str6:
        if 'a' <= x <= 'z':
            x = chr(ord(x) - 32)
            new_str6 += x
        else:
            new_str6 += x
    print(new_str6)
    
  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 '**

    nums = input('Please enter a number:')
    if 0 < int(nums) < 10:
        print('py190100' + nums)
    elif 10 <= int(nums) < 100:
        print('py19010' + nums)
    elif 100 <= int(nums) < 1000:
        print('py1901' + nums)
    else:
        print('Please enter a number less than 1000!')
    
  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

    str8 = input('Please enter a string:')
    count = 0
    for x in str8:
        if not ('0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z'):
            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 = list(input('Please enter a string:'))
    new_str9 = '+'
    for x in str9[1: len(str9)-1]:
        new_str9 += x
    print(new_str9, '+', sep='')
    
  10. Enter a string to get the middle character of the string

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

str10 = input('Please enter a string:')
if len(str10) % 2:
    print(str10[len(str10) // 2])
else:
    print(str10[len(str10) // 2 - 1], str10[len(str10) // 2], sep='')
  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 = input('Please enter the first string:')
str2 = input('Please enter the second string:')
print(str1.find(str2, 0, len(str1)))
  1. Gets the common characters in two strings

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

str12 = input('Please enter the first string:')
str13 = input('Please enter the second string:')
for x in str12:
    if x in str13:
        print(x, end='')
print()

‚Äč

Posted by timon on Sun, 26 Sep 2021 12:23:30 -0700