Python strings are categorized as immutable sequences, meaning that the characters contained in these strings exist in order from left to right, and they cannot be modified locally.
Basic operations
Strings can be merged with the + operator and can be repeated using the * operator.
>>>len ("abc") 3>>> ' abc ' + ' def ' abcdef ' >>> ' ni! ' ' ni! ni! ni! ni! '
A backslash "\" inside a string allows the string to be placed in more than one line.
>>>str = "aaa\ .... bbb\ .... ccc\ .... ddd" >>>straaabbbcccddd
Indexes and Shards
In Python, the characters in a string are extracted by an index.
Shard X[i:j], which means "remove the content from offset to I in X, but not include the offset of J". The result is a new object is returned.
In one Shard, the left boundary defaults to 0, and the right border defaults to the length of the Shard sequence.
S = ' Spam ' >>>s[1:] ' pam ' >>>s ' Spam ' >>>s[:3] ' spa ' >>>s[:-1] ' spa ' >>>s[:] ' Spam ' s[:] Implements a complete copy of the top-level sequence object-an object that has the same value but is a different memory slice. X[I:J:K] means "index x objects in the element, from offset to I until offset to J-1, every k element index once", the third limit K, the default is 1, indicating stepping. You can also use a negative number as a stepping, s[::-1] is actually going to send the sequence. >>>s = ' Hello ' >>>s[::-1] ' Olleh '
String Conversion Tool
The Int function converts a string to a number, and the STR function converts the number to a string representation. The REPR function can also convert an object to its string form, and the returned object will be used as a string of code to recreate the object.
Non-denaturing
A string is an immutable sequence, that is, you cannot modify a string in place, for example, to assign a value to an index. To change a string, you need to create and assign a new string using a tool such as merge, Shard, and, if necessary, assign the result to the original variable name of the string.
>>>s = ' spam ' >>>s[0] = "x" #不允许修改S的值 >>>s = s + "spam" >>>s ' spamspam ' >>>s = ' s Plot ' >>>s = s.replace (' pl ', ' plmal ') >>>s ' Splmalot '