05-08-string and computer foundation

Posted by desoto0311 on Thu, 17 Feb 2022 08:46:17 +0100

summary

1 string

String is a container type data type (sequence), with 'or' 'or' 'or' 'as the container tag

Each independent symbol in the string is an element of the string

The string is immutable; Strings are ordered (subscript operation is supported)

Element of string: each symbol in quotation marks is an element of string. The element of string is also called character. There are two kinds of characters: ordinary character and escape character

Empty string - the element in the string is empty

empty1 = ''
empty2 = ""
empty3 = ''''''
empty4 = """"""
print(type(empty1), len(empty1))    #   <class 'str'> 0
print(type(empty2), len(empty2))    #   <class 'str'> 0
print(type(empty3), len(empty3))    #   <class 'str'> 0
print(type(empty4), len(empty4))    #   <class 'str'> 0

Container flag for string

  • There is no difference between single quotation marks and double quotation marks, and there is no difference between three single quotation marks and three double quotation marks
  • However, the contents in hardening and double quotation marks cannot be wrapped by pressing enter directly
  • The contents of three single quotation marks and three double quotation marks can be wrapped directly through carriage return

Element of string - character

  • Characters are divided into ordinary characters and escape characters

    • Normal character - represents a character that matches 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) - add r/R:r '', R '' before the string
    • Escape character - character encoding

      • Code value - when a computer stores a symbol, it actually stores a fixed number corresponding to the symbol, which is the code value corresponding to the symbol
      • Coding table - a table that matches characters and numbers one by one
        • ASCII code list - there are 128 symbols in total, only American Common symbols, excluding Chinese, numbers in front of letters, uppercase letters in front of lowercase letters (A-65,a-97), and there is a gap between uppercase and lowercase letters
        • Unicode encoding table (Python) - the Unicode encoding table contains ASCII codes; It contains all language symbols (universal code) of all countries and nationalities in the world at present; Chinese code range: 4e00-9fa5
    • Chr (coded value) - get the character corresponding to the coded value

    • Ord (character) - get the encoding value of the specified character (no string with length of 1 is given where characters are required)

  • Fundamentals of computer

    • Memory unit

      • Bit - the smallest unit of computer memory
      • Byte - 8 bits - the minimum unit of memory requested by the program
      • 1kb = 1024 bytes
      • 1mb = 1024kb
      • 1g = 1024mb
    • Base system

      • Binary

      •   '''
          number: 0, 1
          carry:One in every two
          Position right:2**(N-1)
          Representation:   Prefix 0 b
              '''
        
      • Octal

      •   '''
          base: 0, 1,2,3,4,5,6,7
          carry:One in every eight
          Position right:8**(n-1)
          Representation:   Prefix 0 o Or 0 O
          '''
        
      • Decimal

      •   '''
          base:0,1,2,3,4,5,6,7,8,9,
          carry:One in every 10
          Position right:10**(N-1)
          Representation:   direct writing
          '''
        
      • Hexadecimal

      •   '''
          base: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f  (Case is OK
          carry:One in every 16
          Position right:16**(N-1)
          Representation:   Prefix 0 x 0X
          '''
        
  • String related operations

    • check

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

      A space is a character

      An escape character is a character with a length of 1

    • operator

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

      • String * N - repeat the string N times to produce a new string

      •   print('abc' + '123')    # abc123
          print('abc' * 3)    #   abcabcabc
          print('abc' == 'acb')  # False
          print('a' == '\u0061')  #   True
        
      • Compare size: <, >, < =, >=

      • The two strings compare the size of the encoded value of the first pair of unequal characters

      •   print('abc' > 'cd')    # False the comparison between the coded value of a and the coded value of C
        
      • Determine the type of character

      •   '''
          Is it a numeric character : '0' <= char <= '9'
          Is it a lowercase number : 'a' <= char <= 'z'
          Is it a lowercase number : 'A' <= char <= 'Z'
          Is it a letter : 'a' <= char <= 'z' or 'A' <= char <= 'Z'
          Is it Chinese: '\u4e00' <= char <= '\u9fa5'
          '''
        
      • In and not in

        • String 1 in string 2 - determines whether string 2 contains string 1 (determines whether string 1 is a substring of string 2)

        •   print('abc' in 'a1b2c3')    #   False
            print('abc' in 'abc123')    #   True
            print('1' in 'abc123')      #   True
          
      • len, str

        • Str (data) - convert the data into a string (any type of data can be converted into a string. When converting, quote the printed value of the data directly)
  • String related methods

    • .join

      • character string. Join (sequence) - connect the elements in the sequence with the specified string to produce a new string. The elements in the sequence must be strings

      •   ame = ['Xiao Ming', 'Zhang San', 'Li Si']
          
          result = ''.join(name)
          print(result)   #   Xiao Ming Zhang San Li Si
          result = '+'.join(name)
          print(result)   #   Xiao Ming + Zhang San + Li Si
          message = 'abc'
          print('.'.join(message))    #   a.b.c
        
    • . split - cut

      • character string. Split (string 2) - cut string 1 with all string 2 in string 1 as the cutting point, and the cutting point will disappear

      • If the cutting point is at the beginning or end of the string, an empty string will appear, and empty strings will also appear at continuous cutting points. The number of returned strings is one more than the number of cutting points

      • character string. Split (string 2,N) - cut N strings 2 in string 1 as cutting points

      •   str = 'how are you? i am fine, thank you! and you?'
          result = str.split(' ')
          print(result)   #   ['how', 'are', 'you?', 'i', 'am', 'fine,', 'thank', 'you!', 'and', 'you?']
          
          
          result = str.split('you')
          print(result)   #   ['how are ', '? i am fine, thank ', '! and ', '?']
        
    • . replace - 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 N string 2 in string 1 with string 3

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

    str = 'abcd1234'
    str2 = ''
    for x in str[1::2]:
        str2 += x
    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)

    x = input('enter one user name:')
    if 6 <= len(x) <= 10:
        pass
    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 = input('Please enter a user name consisting of numbers and letters:')
    for x in name:
        if 'a' <= x <= 'z' or 'A' <= x <= 'Z' or '0' <= x <= '9':
            pass
        else:
            print('User name is unqualified!')
    
  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

    name = input('Please enter a user name consisting of numbers and letters:')
    for x in name:
        if 'A' <= name[0] <= 'Z':
            if '0' <= x <= '9' or 'a' <= x <= 'z':
                pass
    else:
            print('Illegal 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'

    num = 'abc1shj23kls99+2kkk'
    str = ''
    for x in num:
        if '0' <= x <= '9':
            str += x
    print(str)
    
  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 +'

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

    x = int(input('Please enter 0-999:'))
    y = 1901000
    z = x+y
    if 0 <= x <= 999:
        print('Your student number is:py'+str(z))
    
  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

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

  10. Enter a string to get the middle character of the string

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

  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

  1. Gets the common characters in two strings

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

Topics: Python