Day8 string related operations

Posted by mattonline on Thu, 17 Feb 2022 10:28:48 +0100

Day8 string related operations

  1. character string

    1. What is a string

      • String is a container data type (sequence), with 'or' 'or' 'or' 'as the container flag Each independent symbol is an element of the string

      • String is immutable; String is ordered (subscript operation is supported)

      • Element of string: every symbol in quotation marks is an element of string, which is also called character There are two types of characters: ordinary characters and escape characters

        1. Empty string (nothing in quotation marks, no spaces)

          empty1 = ''
          empty2 = ""
          empty3 = ''''''
          empty4 = """"""
          print(type(empty1), len(empty1))
          print(type(empty2), len(empty2))
          print(type(empty3), len(empty3))
          print(type(empty4), len(empty4))
          
        2. Container flag for string

          • There is no difference between '' and ''; '' and '' '' '' there is no difference
          • The contents of '' and '' cannot be wrapped directly by pressing enter, but only by escape characters
          str1 = 'abc
                 123'
          str2 = '''abd
          123'''
          
          str1 = 'abed, I see a silver light, \n Suspected ground frost'
          str2 = """
          abed, I see a silver light
           Suspected ground frost
          """
          print(str1)
          print(str2)
          
    2. Element of string - character

    • Characters are divided into ordinary characters and escape characters

      • Normal character - a character that represents the symbol itself in a string

      • Escape character - a character that uses \ together with other symbols to represent a special function and meaning

        '''
        \n  -   Line feed
        \t  -   Horizontal tab(amount to tab key)
        \'  -   Represents an ordinary single quotation mark
        \"  -   Represents an ordinary double quotation mark
        \\  -   Represents an ordinary backslash
        '''
        
      • Prevent escape - make all escape characters in the string disappear (make every symbol in a string represent the symbol itself)

        # \u4 bit hexadecimal number - the character corresponding to the encoded value of the hexadecimal number
        
      • Escape character - coded character

        # \u4 bit hexadecimal number - the character corresponding to the encoded value of the hexadecimal number
        
      str1 = '\tabc\n123'
      print(str1)
      
      str2 = 'it\'s me'
      print(str2)
      
      str3 = "it's me"
      print(str3)
      
      str4 = 'C:users\\name\\xiaoming\\next.txt'
      print(str4)
      
      str5 = r'C:users\name\xiaoming\next.txt'
      print(str5)
      
    1. Encoding value - character encoding

      • Coded value - when a computer stores a symbol, it stores a fixed number corresponding to the symbol This number is the coded value corresponding to the symbol
      • Coding table - a table that matches characters and coding values one by one
      '''
      ASCII Code table -   128 symbols in total, Excluding Chinese
                  number(0 - 9 : 48 - 57)
                  capital(A - Z : 65 - 90)
                  Lowercase letters(a - z : 97 - 122)
      Unicode Coding table (python) -   Unicode Code table contains ASCII code; 
                                 The symbols of all nations in the world at present(unicode )
                                 Chinese coding range: 4e00 ~ 9fa5
                      
      chr(Code value)    -   Gets the character corresponding to the encoded value
      ord(character) -   Gets the encoding value of the specified character  (Where characters are needed, Give a string with a length of 1)
      Coded character    -   \u4e00
      '''
      
      print(chr(97))
      print(chr(0x5a89))
      
      print(ord('Yuan'),ord('clock'),ord('Hua'))
      print(hex(34945),hex(38047) ,hex(21326))
      print(chr(0x8881), chr(0x949f), chr(0x534e))
      
      print('a123')
      
      print('------------------')
      
  2. String related operations

    1. check

      • The syntax of getting characters from strings is the same as that of getting elements from lists

        message = 'you see see one day day!'
        print(message[2])
        
      • A space is a character

        str1 = 'abc 123'
        print(str1[4])
        
      • An escape character is a character with a length of 1

        str2 = '\tabc\u4fa9 123'
        print(str2[6])
        
        print(message)
        
        print(message[1:5]) #ou s
        print(message[:-5:-1])  #!yad
        
    2. operator

      • String 1 + string 2 - concatenates two strings to produce a new string

        print('abc' + '123')    #abc123
        
      • String * N - repeat the string N times to produce a new string

        print('abc' * 3)    #abcabcabc
        
      • String comparison size: >, <, > =<=

        • The two strings compare the size of the first pair of unequal character encoding values
        '''
        Is it a numeric character: '0' <= char <= '9'
        Is it lowercase: 'a' <= char <= 'z'
        Is it a capital letter: 'A' <= char <= 'Z'
        Is it a letter: 'a' <= char <= 'z' or 'A' <= char <= 'Z'
        Is it Chinese: '\u4e00'  <= char <= '\u9fa5'
        '''
        
        print('abc' > 'cd') #False
        print('abc' > 'Cd') #True
        

        Exercise: extract all numeric characters in a string

        #'sh function 78sk-- good 2O '- >' good function '
        str1 = 'sh Function 78 sk--2o'
        str2 = ''
        for x in str1:
            if '0' <= x <= '9':
                str2 += x
        print(str2)
        

        Extract Chinese

        # 'sh function 78sk-- good 2O '- >' good function '
        str1 = 'sh Function 78 sk--2o'
        str2 = ''
        for x in str1:
            if '\u4e00' <= x <= '\u9fa5':
                str2 += x
        print(str2)
        
    3. In and not in

      String 1 in string 2 - determines whether string 2 contains string 1

      print('abc' in 'a1b2d3')    #False
      print('abc' in 'abc123')    #True
      print('1' in 'abc123')  #True
      
    4. len and str

      • Str (data) - converts data to a string (any data can be converted into a string, and the printed value shall be enclosed with quotation marks during conversion)

        print(str(100))     #'100'
        print(str(12.5))    #'12.5'
        
        lis1 = [1, 2, 3]
        print(str(lis1))    #'[1, 2, 3]'
        
        dict1 = {'name:' 'Xiao Ming', 'age:',18}
        print(str(dict1))   #"{'name:' 'Xiao Ming', 'age:',18}"
        
  3. String correlation method

    1. character string. Join - concatenates elements in a sequence with a specified string to produce a new string Elements in a sequence must be strings

      names = ['Xiao Ming', 'Zhang San', 'Li Si']
      result = ''.join(names) #Xiao Ming Zhang San Li Si
      print(result)
      
      names = ['Xiao Ming', 'Zhang San', 'Li Si']
      result = ' '.join(names)    #Xiao Ming Zhang San Li Si
      print(result)
      
      names = ['Xiao Ming', 'Zhang San', 'Li Si']
      result = '+'.join(names)    #Xiao Ming + Zhang San + Li Si
      print(result)
      
      message = 'abc'
      print('.'.join(message))    #a.b.c
      
      num = [10, 9, 78, 67]
      result = ''.join([str(x) for x in num])
      print(result)
      
      list1 = ['name', 23, True, 'Hello', 'ha-ha']
      # 'name + Hello + ha ha'
      result = '+'.join(x for x in list1 if type(x) == str)
      print(result)
      
    2. split

      • String 1 Split (string 2) - cut string 1 using all string 2 in string 1 as the cut point

      • String 1 Split (string 2, N) - use the first N string 2 in string 1 as the cutting point to cut string 1

      • Number of cuts = cut points + 1

      • Empty string after cutting: 1 The cutting point is at the top 2 The cutting point is the last 3 Two consecutive cutting points together

        str1 = 'how are you? i am fine , thank you! and you?'
        result = str1.split(' ')
        print(result)   #['how', 'are', 'you?', 'i', 'am', 'fine', ',', 'thank', 'you!', 'and', 'you?']
        
        result = str1.split('you')
        print(result)   #['how are ', '? i am fine , thank ', '! and ', '?']
        
        print('abcaa123amna'.split('a'))    #['', 'bc', '', '123', 'mn', '']
        
        str1 = 'abc12mn12bh12iu12++'
        print(str1.split('12', 3))  #['abc', 'mn', 'bh', 'iu12++']
        
    3. replace

      • String 1 Replace (string 2, string 3) - replaces all string 2 in string 1 with string 3

      • String 1 Replace (string 2, string 3, N) - replaces the first N string 2 in string 1 with string 3

        str1 = 'how are you? i am fine , thank you! and you?'
        
        result = str1.replace('you' , 'me')
        print(result)
        
        result = str1.replace('you', 'me', 1)
        print(result)
        

task

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

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

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

    name_in = input('enter one user name:')
    if 6 < len(name_in) <10:
        print('The user name is legal')
    
    else:
        print('Illegal user name')
    
  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

    name_in = input('enter one user name:')
    for x in name_in:
        if '0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z':
            pass
        else:
            'Illegal name'
            break
    else:
        print('Legal name')
    
  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)

    str =input('enter one user name:')
    
    while 'A' <= str[0] <= 'Z':
        for x in str:
            if '0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <= 'Z':
                pass
            else:
                print('Illegal user name!')
                break
        else:
            print('The user name is legal')
            break
        break
    else:
        print('Illegal user name')
    

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

  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 = ''.join(x for x in str1 if '0' <= x <= '9')
    print(str2)
    
  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 +'

    str1 = 'a2h2klm12+'
    str2 = ''
    print(str1.upper())
    for x in str1:
        if 'a' <= x <= 'z':
            x = chr(ord(x) - 32)
            str2 += x
        else:
            str2 += x
    print(str2)
    
  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'**

    id = 'py1901'
    id_in = input('please enter a number:')
    count = len(id_in)
    for x in range(3-len(id_in)):
        id += '0'
    id_new = id + id_in
    print(id_new)
    
  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
    str = input('Please enter a string:')
    for x in str:
        if not '0' <= x <= '9' or 'a' <= x <= 'z' or 'A' <= x <='Z':
            count +=1
    print('Number of non alphanumeric characters:',count)
    
  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 +'**

    str = input('Please enter a string:')
    while len(str) >= 2:
        str_new = '+' + str[1:-1] + '+'
        print(str_new)
        break
    else:
        print('String length is too short!')
    
  10. Enter a string to get the middle character of the string

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

str = input('Please enter a string:')
num_ = len(str) // 2
if len(str) % 2:
    print(str[num_])
else:
    print(str[num_-1],str[num_])
  1. 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

str = 'how are you? Im fine, Thank you!'
result = str.split('you')
print(len(result[0]))
  1. Gets the common characters in two strings

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

new_str1 = list(str1)
new_str2 = list(str2)
if new_str1 <= new_str2:
    result = [ x for x in new_str1 if x in new_str2]
else:
    result = [x for x in new_str2 if x in new_str1]

print('Common characters are:',result)