Some basic knowledge about string objects in Python, python strings
Python strings are divided into unchangeable sequence classes, meaning that the characters contained in these strings are in the order from left to right, and they cannot be modified locally.
Basic operations
Strings can be merged by using the + operator. You can use the * operator to duplicate the strings.
>>>len("abc")3>>>'abc'+'def''abcdef'>>>'NI!'*4'NI!NI!NI!NI!'
A backslash "\" inside a string can be used to store strings in multiple rows.
>>>str = "aaa\ ....bbb\ ....ccc\ ....ddd">>>straaabbbcccddd
Indexing and partitioning
In Python, the characters in a string are extracted through indexes.
Fragment X [I: J], which indicates that "the offset in X is taken from the offset to I until the content with the offset of J is not included ". The result is a new object.
In a part, the left boundary is 0 by default, and the right boundary is the length of the part sequence by default.
S = 'spam' >>> S [1:] 'pam '>>>> S [: 3] 'pa' >>>> S [: -1] 'pa' >>> S [:] 'spam's [:] implements a full copy of the top-level sequence object-one with the same value, but it is the object of different memory areas. X [I: J: K] indicates "indexing the elements in the x object, from offset to I until offset is J-1, indexing every K element", the third limit K, the default value is 1, indicating a step. You can also use a negative number as a step. The function of S [:-1] is to trigger the sequence. >>> S = 'hello' >>> S [:-1] 'olleh'
String Conversion Tool
The int function converts a string to a number, and the str function converts a number to a string expression. The repr function can also convert an object to its string form. Then, these returned objects will be used as the code string and can be re-created.
Immutable
A string belongs to an immutable sequence, that is, you cannot modify a string in the same place. For example, assign a value to an index. To change a string, you need to use tools such as merging and sharding to create and assign a value to a new string. If necessary, You need to assign this result to the initial variable name of the string.
>>> S = 'spam' >>> S [0] = "x" # The value of S cannot be modified >>>> S = S + "spam" >>> S 'spamspam' >>> S = 'splo' >>>> S = S. replace ('pl', 'plmal')> S 'splmalot'