string

  • A character encoding
  • string
  • String methods (functions)

1 Character encoding

– character encoding

  • ASCII
  • GBK
  • Unicode
  • UTF-8

2 string

What is a string

Strings are one of the most common data types and are defined by enclosing the contents in ‘or “.

Sequence of characters; collection of characters

Python 3 uses Unicode for strings

  • Escape character

The use of \ in a string to escape certain characters for special functions, such as newline \n, TAB \t, ‘represent’, ‘represent’, and so on. You can prefix a string with r without diverting the string \.

cartoon = 'Monty Python's Flying Circus'
cartoon = r'Monty Python's Flying Circus'
Copy the code
  • Three quotes

Strings can also be defined with triple quotation marks, which allow a string to be written across multiple lines without the \ newline character. What you see is what you get

html = """
    <html>
        <head>
        </head>
        <body>
            <h1>Hello, Python</h1>
        </body>
    </html>
"""
Copy the code
  • The subscript

Each character in a string has its own subscript, and we can use the [subscript] operator to get the character at that subscript position.

Print (words[-1]) print gCopy the code
  • immutable

Strings are immutable data types; once defined, their contents cannot be changed.

  • slice

Strings can be truncated using the header subscript: tail subscript operator, also known as slicing.

Print (words[1:]) print(words[1:]) print(words[1:5]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]) print(words[1:-1]Copy the code
  • And not in the in

  • In determines that a string is a substring of another string

  • Not in Determines that a string is not a substring of another string

greeting = "Hello Python"
name = "Python"
if name in greeting:
    print(name, "is a substring of", greeting)
else:
    print(name, "is not a substring of", greeting)
Copy the code
  • for … In the operation

Is a character operation that iterates through a string.

For I in name: print(I)Copy the code

Code Exercise 1

# What is a string - Character set (character sequence) # Special string - no character "" # create string, Mystr = "# empty string mystr = "hello" print(mystr)" "each character in the string is assigned a number, called an index, starting with 0 on the left, Print (mystr[0]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5]) # print(mystr[-5] Print (mystr[4]) # print(mystr[-1]) # print(mystr[5]) # print(mystr[-1]) # print(mystr[5]) Print (mystr[:4]) print(mystr[:4]) print(mystr[:4]) print(mystr[:4]) print(mystr[:4]) Print (mystr[1:]) print(mystr[1:]) Print (mystr[0:5:2]) #[start index: end index: step] # len() ",len(mystr)) # Print (id(mystr)) print(id(mystr)) print(id(mystr)) # escape character # \n - Newline count a character mystr = "line1\nline2\nline3" # \t - TAB character mystr = "w1\tw2\tw3" # \\ - Mystr = "a\ b\ c" print(len(mystr)) # \' - plain '# \" - plain "mystr =" I \" "print(mystr) # Mystr = 'hello' + 'python' # print(mystr) # * string * integer copy string # mystr = "hello"*20 # print(mystr) # in member operator Mystr ="hello Python "print(" Java" in mystr) # not in If the string does not contain the given partial string, Print (" Java "not in mystr) # % Format '' string format symbol %f format floating point number %d format integer %s format string '' name = 'zhang' age = ' 20 PI = 3.1415926 Print ("My name id %s" % name) print("I am %d" % age) print(" PI = %.3f" % PI) print("%d" % int('123'))Copy the code

Code exercise number 2

Mystr ='hello' #[index] - Assigns an integer to each character in the string, starting at 0 on the left, called a positive index, and starting at -1 on the right, Call it negative index h e l l o 0 1 2 3 4 indexes in the front - 5-4-3-2-1 index from behind "' # print (mystr [0]) # print (mystr [5]) # print (mystr [1]) # Print (mystr[0]) #print(mystr[0]) #print(mystr[0]) #print(mystr[0]) #print(mystr[0]) #print(mystr[0]) #print(mystr[0]) But does not include the corresponding character # 3 print (mystr [3] :) # start index defaults to 0 # print (mystr [3:]) # from the start index to the end ' ' '# # # \ n \ escape character - a newline \ t - tabs # \' - a single quote # \" - double quotes "' # mystr =", line1 \ nline2 \ nline3 "# mystr =" w1 \ tw2 \ tw3 "# mystr = 19.8 \ '\', \ '\' 2.0" # print (mystr) '+' string operator String concatenation * repeatedly prints the string in member operator, returning True not in if the string contains the given character, If the string does not contain the given character return True % format string "" # mystr="hello" +","+"123" # mystr="hello"*10 # print(mystr) # mystr=" helloABC" # b = "ABC" in mystr # b = "aaa" not in mystr # print(b) # % string format %f string format %d string format %s string format "" # # print("I am %d" % age) # print(" PI is %.3f" % PI) # print(" PI is %.3f" % PI) # print("I am %d" % age) # print(" PI is %.3f" % PI) Name = "zhang" print(f"hello {name}")Copy the code