Basic string operations all standard sequence operations (indexing, partitioning, multiplication, determining membership, length, minimum value, and maximum value) are also applicable to strings. strings are unchangeable, the partition assignment is not legal: [python] website = 'HTTP: // blog.csdn.net/grassroots2011.net' website [-3:] = 'com' string formatting applies to the string formatting operator, namely, percent sign %. Place a string (formatted string) on the left side of %, and the value to be formatted on the right side. [Python] format = 'hello, % s, % s enough for ya? 'Values = ('World', 'hot ') print (format % values) Output: Hello, world, Hot enough for ya? Note: % s is called the conversion specifier, and s indicates that the value will be formatted as a string-if it is not a string, it is converted to a string ====================================== format a real number (floating point number ), use the f specifier type to provide the required precision: A period plus the number of decimal places to be retained. Because the formatting specifier always ends with a type character, the precision should be placed before the type character: [python] format = 'Pi with three decimals: %. 3f' from math import pi print (format % pi) Output: Pi with three decimals: 3.142 ================================ the template string module provides another format value Method: template string. It works like replacing variables in many UNIX shells. For example, the substitute Template method replaces $ foo [python] from string import Template s = Template ('$ x, glorious $ x! ') S. substitute (x = 'slurm') Output: slurm, glorous slurm! ============================== Replace the first part of the field with {} [python] s = Template ("It's $ {x} tastic ") s. substitute (x = 'slurm ') ============================ you can use $ to insert the dollar symbol [python] s = Template ("Make $ selling $! ") S. substituts (x = 'slurm ') ======================================== except for the keyword parameter, you can also use dictionary variables to provide value/name pairs for [python] s = Template ("A $ thing must never $ action ") d = {} d [thing] = 'gentleman 'd [action] = 'show his socks' s. substituts (d) string format conversion type d, i-Signed decimal integer o unsigned octal u unsigned decimal x unsigned hexadecimal -- lower case X unsigned hexadecimal -- floating point represented by the upper case e scientific notation -- floating Point number represented by lower case E scientific notation-upper case f, if the F-decimal floating point g index is greater than-4 or less than the precision value, it is the same as e. Otherwise, G is the same as f. If the index is greater than-4 or less than the precision value, it is the same as E, in other cases, the C single character r string is the same as F (using repr Converting any Python object) s string (using str to convert any Python object) 1. Simply converting www.2cto.com simply requires writing the conversion type. It is easy to use [python] 'price of eggs: % d' % 42 output: Price of eggs: 42 2. Field width and precision conversion specifiers can include field width and precision. The field width is the minimum number of characters retained by the converted value, the precision is the number of decimal places that the result should contain, or the maximum number of characters that the converted value can contain. These two parameters are all integers (first, the character width, and then the precision), separated by periods. [Python] '% 10f' % 42 # field width 10' % 10.2f' % 42 # field width 10 precision 2' %. 2f '% 42 # precision 2 can use * as the field width or precision [python]' %. * s '% (5, 'guido van rosblum') Output: 'guid'