String basic string operations
Strings are also sequences. Therefore, the basic operations of sequences (index, partition, join, multiplication, length, maximum and minimum values, and membership) are also applicable to strings:
Index
>>> 'A _ string' [0]
'A'
Length
>>> Len ('A _ string ')
8
Maximum Value
>>> Max ('A _ string ')
'T'
Minimum value
>>> Min ('A _ string ')
'_'
Multiplication
>>> 'A _ string' * 2
'A _ stringa_string'
Connection
>>> 'A _ string' + 'another _ string'
'A _ stringanother_string'
Parts
>>> 'A _ string' [2: 6]
'Stri'
>>> 'A _ string' []
'_ R'
Assignment
>>> 'A _ string' [3] = 'B'
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: 'str' object does not support item assignment
Because the string cannot be modified, the value assignment operation is not available for the string.
String formatting
String formatting is implemented by the string formatting operator %.
Place a string (formatted string) on the left side of %, and the value to be formatted on the right side. You can use a value, such as a string or number:
>>> Print "Hello, % s! "% 'Signature'
Hello, signjing!
>>> Age = 20
>>> Print "I'm % d years old! "% Age
I'm 20 years old!
You can also use tuples or dictionaries with multiple values. Use tuples in general:
>>> Format = "Hello, % s, % s enough for ya? "
>>> Values = ('World', 'hot ')
>>> Print format % values
Hello, world, Hot enough for ya?
If you use a list or another sequence instead of a tuples, the sequence is interpreted as a value. Only tuples and dictionaries can format more than one value.
The % s part of the formatted string is called the conversion specifiers, which mark the location where the conversion value needs to be inserted. S indicates that the value is formatted as a string. If it is not a string, str is used to convert it to a string. This method is valid for most values.
If you want to include the percent sign in the formatted string, you must use %. python will not mistakenly think of the percent sign as a conversion specifier.
>>> Print "% s" % 'test'
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: not enough arguments for format string
>>> Print "% s" % 'test'
Test % s
>>> Print "% s" % 'test'
% S test
If you want to format a real number (floating point number), you can use the f specifier type and provide the required precision: A period plus the number of decimal places you want to retain. Because the formatting specifiers always end with a type character, the precision should be placed before the type character:
>>> Print "PI is %. 2f" % 3.1415926
PI is 3.14
The right operand of the formatting operator can be anything. If it is a tuples or ing type (such as a dictionary), the string formatting will be different.
If the right operand is a tuples, each element is formatted separately, and each value requires a corresponding conversion specifier. If the tuples to be converted exist as part of the conversion expression, you must enclose them in parentheses to avoid errors:
Correct
>>> '% S plus % s is % s' % (12, 3, 4)
'12 plus 3 is 4'
Error
>>> '% S plus % s is % s' % 12, % 3, % 4
File" ", Line 1
'% S plus % s is % s' % 12, % 3, % 4
^
SyntaxError: invalid syntax
>>> '% S plus % s is % s' % 12% 3% 4
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: not enough arguments for format string
>>> '% S plus % s is % s' % 12% 3% 4
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: not enough arguments for format string
Basic conversion specifiers
The basic conversion specifiers (excluding the ing key specifiers) include the following [the order of these items is crucial]:
% [Conversion flag] [minimum field width] [. Precision value] Conversion Type
% Characters:
Mark the start of the conversion specifier
Conversion flag (optional ):
-Left alignment;
+ Indicates that the positive and negative signs must be added before the conversion value;
"" (Blank character) indicates that spaces are reserved before a positive number;
0 indicates that the conversion value is filled with 0 if the number of digits is not enough;
Minimum field width (optional ):
The converted string must have at least the specified width.
If it is *, the width is read from the value tuples.
Point (.) followed by precision value (optional ):
If the conversion is a real number, the precision value indicates the number of digits after the decimal point.
If the conversion is a string, the number indicates the maximum field width.
If it is *, the precision will be read from the tuples.
Conversion Type: See the following table;
String format conversion type
Conversion Type meaning
D, I-Signed decimal integer
O unsigned octal
U unsigned decimal
X non-Signed hexadecimal (lower case)
X unsigned hexadecimal (uppercase)
E. Floating-point numbers represented by scientific notation (lower case)
E. floating point number represented by scientific notation (uppercase)
F, F decimal floating point number
If the g index is greater than-4 or smaller than the precision value, it is the same as e. Otherwise, it is the same as f.
If the G index is greater than-4 or smaller than the precision value, it is the same as E. Otherwise, it is the same as F.
C single character (accept integer or single character string)
R string (use repr to convert any python object)
S string (use str to convert any python object)
Simple Conversion
A signed decimal integer
>>> "Price is $ % d" % 42
'Price is $42'
>>> "Price is $ % I" % 42
'Price is $42'
Unsigned octal chart
>>> "Number is % o" % 42
'Number is 52'
Unsigned decimal
>>> "Number is % u" % 100
'Number is 100'
Unsigned hexadecimal format (uppercase)
>>> 'Number is % x' % 155
'Number is 9b'
Non-Signed hexadecimal format (lower case)
>>> 'Number is % x' % 155
'Number is 9b'
Floating Point number represented by scientific notation (lower case)
>>> 'Number is % E' % 155
'Number is 1.5520.e + 02'
Floating Point number represented by scientific notation (uppercase)
>>> 'Number is % E' % 155
'Number is 1.5520.e + 02'
Decimal floating point number
>>> 'Number is % F' % 155
'Number is 155.000000'
>>> 'Number is % F' % 155
'Number is 155.000000'
If the index is greater than-4 or smaller than the precision value, it is the same as E. Otherwise, it is the same as F.
>>> 'Number is % G' % 0.000000126
'Number is 1.26E-07'
>>> 'Number is % G' % 0.000000126
'Number is 1.26e-07'
>>> 'Number is % G' % 126
'Number is 126'
>>> 'Number is % G' % 126
'Number is 126'
>>> 'Number is % G' % 1260000000000
'Number is 1.26e + 12'
>>> 'Number is % G' % 1260000000000
'Number is 1.26E + 12'
Single Character (accept INTEGER (0 ~ 255) or single character string)
>>> 'Number is % C' % 123
'Number is {'
>>> 'Number is % C' % 'B'
'Number is B'
>>> 'Number is % C' % 255
'Number is \ xff'
>>> 'Number is % C' % 256
Traceback (most recent call last ):
File" ", Line 1, in
OverflowError: unsigned byte integer is greater than maximum
>>> 'Number is % C' % 0
'Number is \ x00'
>>> 'Number is % C' %-1
Traceback (most recent call last ):
File" ", Line 1, in
OverflowError: unsigned byte integer is less than minimum
String (use str to convert any python object)
>>> 'Using str: % s' % 12L
'Using str: 12'
String (use repr to convert any python object)
>>> 'Using repr: % R' % 12L
'Using repr: 12l'
Field width and precision
Conversion specifiers can include character width and accuracy.
Character width:
Is the minimum number of characters retained by the converted value,
Precision:
(For digits) is the number of decimal places in the result;
(For String Conversion) is the maximum number of characters contained in the converted value;
These two parameters are all integers (first, the character width, and then the precision), separated by periods. Although both are optional parameters, if only precision is given, it must contain the vertex number:
>>> From math import pi
>>> '% 10f' % pi
'123'
>>> '% 10.2f' % pi
'123'
>>> '%. 2f' % pi
'3. 14'
>>> '%. 5s' % 'signjing'
'Signj'
You can use * (asterisk) as the field width or precision (or both). In this case, the value is read from the parameter of the tuples:
>>> '%. * S' % (5, "signjing ")
'Signj'
>>> '%. * S' % (7, "signjing ")
'Signatjin'
Symbol, alignment, and 0 Filling
Before the field width and precision value, you can place a "standard table", which can be zero, plus, minus, or space. 0 indicates that the number is filled with 0.
>>> '% 10.2f' % pi
'123'
>>> '% 010.2f' % pi
'2014. 14'
>>> '% + 010.2f' % pi
'+ 123'
>>> '%-010.2f' % pi
'3. 14'
>>> '% 10.2f' % pi
'123'
Note:
The value 0 in the header in 010 does not mean that the field width is octal. It is just a common python value.
When 010 is used as the character width specifier, it indicates that the field width is 10, and 0 is used to fill the space, rather than 8:
>>> 010
8
Minus sign (-) is used to align values left:
Blank ("") means adding a space before a positive number, which is very useful when you need to correct a positive number.
>>> '%. 2f' % pi
'123'
>>> '%. 2f' %-pi
'-3.14'
The plus sign (+) indicates that both positive and negative numbers indicate symbols (also useful in alignment ):
>>> Print ('% + 5d' % 10) + '\ n' + (' % + 5d '%-10)
+ 10
-10
String Method
There are too many string methods. Here we only list a few useful methods.
Find
The find method can search for a substring in a long string and return the leftmost index of the substring location, if no value is found,-1 is returned (if the index method of the list or tuples is not found, an error is returned ).
>>> 'I \'m a sunny boy'. find ('sun ')
6
>>> 'I \'m a sunny boy'. find ('sunb ')
-1
The find method can also accept optional start and end points parameters:
Only the starting point is provided:
>>> 'I \'m a sunny boy'. find ('sun', 5)
6
>>> 'I \'m a sunny boy'. find ('sun', 9)
-1
The start and end points are provided.
>>> 'I \'m a sunny boy'. find ('sun', 5, 7)
-1
>>> 'I \'m a sunny boy'. find ('sun', 5, 9)
6
Remember, just like the sharding operation in the list, the 2nd and 3rd parameters of the find method include the index value of the second parameter, but do not include the index value of the 3rd parameter, this is a convention in python.
>>> 'I \'m a sunny boy'. find ('sun', 6, 9)
6
>>> 'I \'m a sunny boy'. find ('sun', 6, 8)
-1
Join (very important)
The join method is a very important string method. It is the inverse method of the split method, used to add elements to the queue:
>>> Seq = [1, 2, 3, 4, 5]
>>> Sep = '+'
>>> Sep. join (seq)
Traceback (most recent call last ):
File" ", Line 1, in
TypeError: sequence item 0: expected string, int found
>>> Seq = ['1', '2', '3', '4', '5']
>>> Sep = '+'
>>> Sep. join (seq)
'1 + 2 + 3 + 4 + 5'
It can be seen that all the queue elements to be added must be strings.
Lower
The lower method returns the lower-case response of the string:
>>> "Signjing". lower ()
'Signature'
>>> "Signjing". lower ()
'Signature'
This method is useful if you want to write code that is case-insensitive.
Replace
The replace method returns the string after all matching items of a string are replaced.
>>> "Signjing". replace ("Si", "fin ")
'Fingnjing'
>>> "This is a test". replace ("is", "eez ")
'Theez eez a Test'
Split (very important)
It is the inverse method of join, used to divide strings into sequences.
>>> 'Id, index, name, info'. split (',')
['Id', 'index', 'name', 'info']
If no separator is provided, the program uses all spaces as separators (spaces, tabulation, line breaks, etc ).
>>> 'Id index name info'. split ()
['Id', 'index', 'name', 'info']
>>> 'Id index name info'. split ()
['Id', 'index', 'name', 'info']
Strip
The strip method returns a string that removes spaces on both sides (excluding internal spaces:
>>> "Space". strip ()
'Space space'
The strip method and the lower method can be used together to conveniently compare the input and stored values.
You can also specify the characters to be removed:
>>> 'Space space'. strip ('E ')
'Space spac'
>>> 'Space space'. strip ('se ')
'Pace spac'
Translate
The translate method is the same as the replace method. Some parts of the string can be replaced. However, unlike the latter, the translate method only processes a single character. Its advantage is that it can carry out multiple replicas at the same time, and sometimes it is much more efficient than replace.
Before using the translate conversion function, you must complete a conversion table. The conversion table replaces the correspondence between a character and a character. Because this table (actually a string) has up to 256 Items, you can directly use the maketrans function in the string module.
The maketrans function accepts two parameters: two equal-length strings, indicating that each character in the first string is replaced by a character in the same position in the second string.
>>> From string import maketrans
>>> Table = maketrans ('cs ', 'kz ')
>>> Len (table)
256
>>> Table
'\ X00 \ x01 \ x02 \ x03 \ x04 \ x05 \ x06 \ x07 \ x08 \ t \ n \ x0b \ x0c \ r \ x0e \ x0f \ x10 \ x11 \ x12 \ x13 \ x14 \ x15 \ x16 \ x17 \ x18 \ x19 \ x1a \ x1b \ x1c \ x1d \ x1e \ x1f! "# $ % & \ '() * +,-./0123456789:; <=>? @ ABCDEFGHIJKLMNOPQRSTUVWXYZ [\] ^ _ 'abkdefghijklmnopqrztuvwxyz {| }~ \ X7f \ x80 \ x81 \ x82 \ x83 \ x84 \ x85 \ x86 \ x87 \ x88 \ x89 \ x8a \ x8b \ x8c \ x8d \ x8e \ x8f \ x90 \ x91 \ x92 \ x93 \ x94 \ x95 \ x96 \ x97 \ x98 \ x99 \ x9a \ x9b \ x9c \ x9d \ x9e \ x9f \ xa0 \ xa1 \ xa2 \ xa3 \ xa4 \ xa5 \ xa6 \ xa7 \ xa8 \ xa9 \ xaa \ xab \ xac \ xad \ xae \ xaf \ xb0 \ xb1 \ xb2 \ xb3 \ xb4 \ xb5 \ xb6 \ xb7 \ xb8 \ xb9 \ xba \ xbb \ xbc \ xbd \ xbe \ xbf \ xc0 \ xc1 \ xc2 \ xc3 \ xc4 \ xc5 \ xc6 \ xc7 \ xc8 \ xc9 \ xca \ xcb \ xcc \ xcd \ xce \ xcf \ xd0 \ xd1 \ xd2 \ xd3 \ xd4 \ xd5 \ xd6 \ xd7 \ xd8 \ xd9 \ xda \ xdb \ xdc \ xdd \ xde \ xdf \ xe0 \ xe1 \ xe2 \ xe3 \ xe4 \ xe5 \ xe6 \ xe7 \ xe8 \ xe9 \ xea \ xeb \ xec \ xed \ xee \ xef \ xf0 \ xf1 \ xf2 \ xf3 \ xf4 \ xf5 \ xf6 \ xf7 \ xf8 \ xf9 \ xfa \ xfb \ xfc \ xfd \ xfe \ xff'
>>> Table [97: 123]
'Abkdefghijklmnopqrztuvwxy'
>>> Maketrans ('','') [97: 123]
'Abcdefghijklmnopqrstuvwxy'
>>> Maketrans ('cs ', 'kz') [97: 123]
'Abkdefghijklmnopqrztuvwxy'
After the table is created, it can be used as a parameter of the translate method. The conversion of strings is as follows:
>>> 'This is an incredible test '. translate (table)
'Thiz iz an inkredible tezt'
The second parameter is optional and is used to specify the characters to be deleted:
>>> 'This is an incredible test '. translate (table ,'')
'Thizizaninkredibletezt'
>>> 'This is an incredible test '. translate (table, 'thi ')
'Z z an nkredble ez'