Chapter 3 string Learning
1. The string cannot be modified, and the multipart assignment cannot be performed.
>>> Format = "hello, % s. % s enough for ya? ">>> Values = ('World', 'hot ') >>> print (format % values) # place a string (formatted string) on the left of % ), place the value hello, world on the right. hot enough for ya? >>> Format = "Pi with three decimals: %. 3f "#. 3 indicates the number of decimal places to be retained. For the f table type (floating point number) >>>> from math import pi >>> print (format % pi) Pi with three decimals: 3.142 Template string -- Template --- substitute >># Template string >>> from string import Template >>> s = Template ('$ x, glorious $ x! ') >>> S. substitute (x = 'slurm')' slurm, glorous slurm! '>>># If the field to be replaced is part of a word, you need to add {}>> s = Template ("It's $ {x} tastic! ") >>> S. substitute (x = 'slurm')" It's slurmtastic! ">>># You can use $ to insert A dollar sign >>># use A dictionary variable to provide A value/name pair >>> s = Template ('A $ thing must never $ action. ') >>> d = {}>>> d ['action'] = 'gentleman '>>> d ['action'] = 'show his socks' >>> s. substitute (d) 'A gentleman must never show his socks. '2. String formatting >>> '% s plus % s equals % s' % (, 2) # Use tuples instead, note that you cannot discard the brackets '1 plus 1 equals 2' >>>> 'price of eggs: $ % d' % 42 # d. I indicates the signed decimal integer 'price of eggs: $42 '>>> 'hexadecimal price of eggs: % x' % 42 # Not signed In Hexadecimal (lower case), X (upper case) 'hexadecimal price of eggs: 2a '>>> from math import pi >>> 'Pi: % f... '% pi # f/F decimal floating point 'Pi: 3. 141593... '>>> 'very inexact estimate of pi: % I' % pi # I signed decimal integer 'very inexact estimate of pi: 3. segment width and accuracy >>## width: the minimum number of characters retained by the converted value. >>> # Precision: the number of decimal places to be included, or the maximum number of characters After conversion. (String) >>> '% 10f' % pi # field width 10' 3.141593'> '% 10.2f' % pi # field width 10' 3.14 '>' %. 2f '% pi # accuracy 2' 3. 14'> '%. 5s '% 'Hello python' # precision 5. For a string, it is up to five characters 'hello' >>>' %. * s '% (5, 'Hello python') # Use * to indicate precision, value 'hello' 4, symbol, alignment, and 0 fill in the tuples -- a table can be placed before the field width and precision, this table can be zero, plus sign, minus sign, or space >>>> from math import pi >>>' % 10f' % pi '000000' >>>' % 010.2f '% pi '2017. 14' >>> '%-10.2f' % pi #-indicates left alignment '3. 14 >>> print ('% 5d' % 10) + '\ n' + (' % 5 D' %-10) # space. Add a blank space before a positive number to align it with a negative number. 10-10 >>> print ('% + 5d' % 10) + '\ n' + (' % + 5d '%-10) # (+) plus sign, both positive and negative numbers mark the symbol + 10-105. The string method # The find method can search for the substring in a long string and return the leftmost index of the substring location. -1 >>> title = "Monty python" s Flying Circus ">>> title. find ('monty ') 0 >>> title. find ('python') 6 >>> subject = '$ Get rich now !!! $ '>>> Subject. find ('$') 0 >>> subject. find ('$', 1) # provides the starting point, starting from 1, 20 >>> subject. find ('!!! ', 0, 16) # provide the start and end points, including the start index, excluding the end index-1 #-1 indicates that the join method is not found. # The join method is the inverse method of the split method, used to connect to the list. It can only be a string >>> seq = ['1', '2', '3', '4 ', '5'] >>> sep = '+' >>> sep. join (seq) '1 + 2 + 3 + 4 + 5' >>>>>> dirs = '', 'usr', 'bin ', 'env'> '/'. join (dirs) '/usr/bin/env'> print ('C: '+ '\\'. join (dirs) C: \ usr \ bin \ env # The lower method returns the lower-case character of the string >>> 'she IS tall '. lower () 'She is tall' # The title method converts the first letter of the string to uppercase, And the other letters to lowercase >>> "that's all folks ". title () "That's All Folks" # capwords function of the String module: converts All letters to uppercase >>> import string >>> string. capwords ("that's all folks ") "That's All Folks" >>> # The replace method returns the string obtained after All matching items of a string are replaced >>> 'this is a test '. replace ('is', 'eez') 'theez eez a test' # The split method is the inverse method of join, splits a string into a sequence> '1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 '. split ('+') ['1', '2', '3', '4', '5', '6', '7', '8 ', '9']> '/usr/bin/env '. split () ['/usr/bin/env']>'/usr/bin/env '. split ('/') ['', 'usr', 'bin', 'env']> 'Using the default '. split () ['using', ''the, 'default'] # the strip Method returns a string that removes spaces on both sides (excluding internal spaces) >>> 'internal whitespace is kept '. strip () 'internal whitespace is kept '# translate () is the same as replace (), but can only process a single character