Python sequence 2-basic operation of string strings
Strings are a string of characters enclosed in single or double quotation marks, which is the most basic type of data. Its basic operations include: create assignment, access, change, delete . An example of these methods illustrates
#创建赋值
>>>astring =' Hello word! '
#访问
>>>astring[0]
' H '
>>>astring[1:4]
' ell '
#改变
>>>astring = astring[:6] +' Python '
>>>astring
' Hello Python '
#删除, empty or delete a string by assigning a null character or by using the DEL statement
>>>astring ="'
>>>astring
"'
>>>delAstring
跟数字类型一样,字符串类型也是不可变的,也就是说不能通过值改变一个字符串的一个字符或者一部分字符串,但是可以通过拼凑一个旧串的各个部分来得到一个新串。
The operator of the string
Strings are part of a sequence, so some of the basic operations that can be used in a sequence are normal here, no longer a detailed example, just a general list, and see the first of the sequence in detail. The details include the following:
Standard type operators: > < = etc Some comparison and assignment operations
Sequence operator: Slice ([]), member operator (in, not in), connector (+), repeating operator (*)
Operators that apply only to strings
Format operator%
The format operator% can only be used in the string, the purpose is to be able to bring into a desired object, the operation is generally a% followed by an English letter, commonly used are:
%d to signed decimal integer
%u turns to unsigned decimal integer
%s takes precedence over character conversion with the STR () function
%f converted to floating point
% OUTPUT%
Built-in functions
The built-in function is also described in detail in the previous article, which is still broadly listed here, specifically including
CMP (x, y) X>y is return 1, x=y returns 0, X<y returns-1
Len (SEQ) returns the length of the object seq
Max () and Min () return the maximum value inside the string, the minimum value
Enumerate (str), each of the string str is paired to iterate, the first shows the number, the second shows the item
Zip (str1, str2), which consists of a tuple of identical strings, placed in a list
String type functions
Raw_input () Prompts the user for input and returns a string format
Chr () receives 0-255 of the number, returning a letter
String built-in functions
There are too many built-in functions for strings, but the most basic, this one has been introduced in this article, and the function of the string, you can directly refer to the appendix of the Basic Python tutorial, the explanation, no longer explained in detail
Unique characteristics of strings
string invariance
A string is an immutable data type, which means that its value cannot be changed or modified. This means that if you want to modify a string, or intercept a substring, or concatenate another string at the end of a string, you must create a new string.
Python sequence 2-string