No. 03 Chapter: Using Strings
------
Supported operations
- Index
- Slice
- add element
- Delete Element
- Updating elements
- Find elements (check if an element is part of a sequence)
- Sequence length
- Maximum Value
- Minimum value
- Other built-in functions
>>> website= ' http://www.python.org ' >>> website[-3:]= ' com ' #此操作不合法 because the string is immutable and cannot be modified Traceback (most recent): File "<pyshell#162>", line 1, in <module> website[-3:]= ' com ' TypeError: ' Str ' object does not support item assignment
------
string formatting:
%s: Conversion description Fu Yuanzu
#字符型 >>> str1 = "Hello,%s.%s enough for ya?" >>> val1 = (' Jerry ', ' hot ') >>> print str1% Val1hello, Jerry. Hot enough for ya? #浮点型 >>> format = ' PI with three decimals:%.3f ' >>> from math import pi>>> Print format% pipi with three decimals:3.142note:1. With a list, the sequence is interpreted as a value, and only Ganso and dictionaries can format more than one value of 2. Percent semicolon:% template string # Similar to variable substitution in Linux, Template,substitute (convert parameters to pass) >>> s = Template (' $x, glorious $x! ') >>> S.substitute (x= ' Slurm ') ' slurm,glorious slurm! ' #如果是单词的一部分的话, enclosed in {} >>> s = Template (' I love ${x}rry! ') >>> S.substitute (x= ' She ') ' I love sherry! ' #如果是美元, note that $$ is used to represent the dollar symbol, Safe_substitute does not error >>> s = Template (' Using $$ buy ${x}! ') due to missing value or dollar symbol >>> S.substitute (x= ' food ') ' Using $ buy food! ' #利用字典提供键/value pairs >>> 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 ' >>> s = Template (' I am ${name},${age} years old. ') >>> d = {' name ': ' Jerry ', ' Age ':50}>>> S.substitute (d) ' I am jerry,50 years old. ' #>>> s = '%s plus%s equals%s '% (1,1,2) >>> print s1 plus 1 equals 2------
Basic Conversion Description Symbols:
(1)% character, beginning of the symbol conversion
(2) Conversion flag (optional)
- -Align Left
- + positive and negative numbers before conversion values
- "" White space character, before a positive number, preserves space
- 0 if the conversion value is not enough, fill with zeros
(3) Minimum field width (optional):
- Minimum width with the specified value
- If it is *, the value is read from the meta-ancestor
(4). Accuracy value (optional)
- If it is a real number, the precision value represents the following digits
- If it is a character, the precision value indicates the following maximum width
- If it is *, the precision value is read from the meta-ancestor
(5) Conversion type
------
------
------
------
------
------
------
------
------
<<python Basic Tutorials >> Learning Notes | No. 03 Chapter | String