3.1 Basic string manipulation: all standard sequence Operations (index, Shard, multiply, Judge membership, length, minimum, maximum) are equally valid for strings. However, keep in mind that strings are immutable 3.2 string formatting: The Lite string formatting is implemented using the string formatting operator, percent%. % can also be used as a MO operation (redundancy) operator. Place a string (formatted string) on the left side of%, and the right side places the value you want to format. You can use a value, such as a string or a number, or a tuple of multiple values or a dictionary. In general, tuples are used:
" hello,%s,%s enough for ya? ">>> values = ('World','hot') print format% for ya?
Note: 1. If you use lists or other sequences instead of tuples, the sequence is interpreted as a value. Only tuples and dictionaries can format more than one value. 2. The%s portion of the formatted string is called the conversion specifier, and they 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, it is converted to a string using str. 3. If you want to include a percent semicolon in a formatted string, you must use In this way, Python will not mistake the percent sign as the conversion specifier 4. If you are formatting real numbers (floating-point numbers), you can use F to describe the type of conversion specifier, providing the required precision: a period plus the number of decimal digits you want to keep. Because the format conversion specifier always ends with a character that represents a type, the precision should precede the type character:
" Pi with three decimals:%.3f " from Import Pi print format%3.142
The 3.2.1 template string provides another way to format a value: a template string. It works like a variable substitution in many Unix shells. As follows: Substitute this template method replaces the $foo in the string with the passed in keyword foo:
from Import template>>> s = Template ('$x, glorious $x! ' )>>> s.substitute (x='slurm')'Slurm, Glorious slurm! '
If a part of the word is replaced, then the parameter name must be enclosed in parentheses to accurately indicate the end:
>>> s = Template ("It ' s ${x}tastic")>>> s.substitute (x=' Slurm')"It ' s slurmtastic"
You can use $$ to insert dollar signs:
>>> s = Template ("make$$ selling $x! " )>>> s.substitute (x='slurm')'make$ selling slurm! '
In addition to the keyword parameters, you can use the key value pairs provided by the dictionary
>>> s = Template ( " a $thing must never $action. " ) >>> d = {} >>> D[ " thing " ] = " gentleman " >>> d[" action ' ] = " show his socks " >>> S.substitute (d) " a gentleman must never show his socks. "
Method Safe_substitute does not cause a missing value or incorrect use of the $ character 3.3 string formatting: The full version of the right operand of the format operator can be any type, if it is a tuple or mapping type (such as a dictionary), then the string formatting will be different. If the right operand is a tuple, then each of these elements will be individually formatted, and each word will need a corresponding conversion specifier if the tuple that needs to be converted exists as part of the transformation expression, you must enclose it in parentheses to avoid errors
>>> " %s plus%s equals%s Span style= "COLOR: #800000" > "% (1,2,3 1 plus 2 equals 3 " >>> " %s plus% equals%s. "% 1,2,3traceback (most recent call last): File
" <STDIN> ", line 1, in <module>typeerror: not enough arguments for format string
3.3.1 Basic conversion specifier the basic conversion specifier includes the following sections. Note that the order of these items is of paramount importance to the 1.% character: The start of the token conversion specifier 2. Conversion flag (optional): "-" for left-aligned, "+" for sign before converting value; "" (white space character)A blank space is reserved before a positive number, and 0 indicates that the conversion value is filled with 0 if there are not enough digits3. Minimum field width (optional): The converted string should have at least the width specified by the value, and if it is *, the width will be read from the value tuple4. Point (.) followed by precision (optional): If the converted real number, the precision represents the digits after the decimal point, if the conversion is a string, then the number represents the maximum field width. If it is *, then the precision will be read from the tuple5. Conversion type: As shown below
Conversion type |
Meaning |
D,i |
Signed decimal integer |
O |
Octal with no symbols |
U |
Non-Signed decimal |
X |
unsigned hexadecimal (lowercase) |
X |
unsigned hexadecimal (uppercase) |
E |
Floating-point numbers represented by science and technology Law (lowercase) |
E |
Floating-point numbers represented by science and technology Law (uppercase) |
F,f |
Decimal floating-point number |
G |
If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as E |
G |
If the exponent is greater than-4 or less than the precision value is the same as E, the other case is the same as E |
C |
Single-character (receive integer or single character string) |
R |
String (convert any Python object using repr) |
S |
String (convert any Python object using str) |
3.3.2 Simple conversion simple conversion only need to write out the conversion type, easy to use:
>>>'Price of eggs:%d'% 42'Price of eggs:42'>>>'hexadecimal price of eggs:%x.'% 42'hexadecimal price of eggs:2a.'>>> fromMathImportPi>>>'Pi:%f ...'%Pi'pi:3.141593 ...'>>>'Pi:%f ...'%Pi'pi:3.141593 ...'>>>'Very inexact estimate of pi:%i.'%Pi'Very inexact estimate of Pi:3.'>>>'Using str:%s.'% 42L'Using str:42.'>>>'Using repr:%r.'% 42L'Using repr:42l.'
3.3.3 Field width and precision conversion specifiers can include field widths and precision. The field width is the converted value the minimum number of characters that the clubhouse retains, and the precision (for a numeric conversion) is the number of decimal digits that should be included in the result, or (for string conversions) the maximum number of characters that the converted value can contain. Both parameters are integers (first the field width, then the precision), separated by a dot (.). Although both are optional parameters, you must include the dot number if you only give precision:
>>>'%10f'% PI#Field width Ten'3.141593'>>>'%10.2f'% PI#field width 10, accuracy 2'3.14'>>>'%.2f'% PI#Accuracy 2'3.14'>>>'%.5s'%'Guido van Rossum''Guido'
You can use * (asterisk) as the field width or precision (or both), and the values are read from the tuple parameter:
' %.*s '% (5,'Guido van Rossum')'Guido '
3.3.4 symbol, for alignment with 0 fill the field width and precision can also be placed before a "flag", the flag can be zero, plus, minus, or a space. A zero indicates that the number will be populated with 0. Note: The 0 that starts with 010 does not mean that the field width specifier is an octal number, he is just a normal Python value. When you use 010 as the field width specifier, the field width is 10, and the empty space is filled with 0, not the field width of 8.
Python Note 03: Using Strings