Class Method string. Upper (STR) needs to introduce the string module, the instance method Str. Upper () does not need to introduce the string Module
Unparalleled list resolution
# Coding = UTF-8 # list parsing print [I * 2 for I in [8,-2, 5] print [I for I in range (8) if I % 2 = 0]
In FF's javascript1.7, the same syntax is implemented (array comprehension)
VaR evens = [I for (I in range (0, 21) if (I % 2 = 0)];
String Template
# Coding = UTF-8 # string template from string import templates = template ('there are $ {howmany }$ {Lang} quotation symbols') print S. substitute (lang = 'python', howute = 3)
This also exists in javascrit. For example, the famous prototype has this template class. The difference is that its replacement function is called evaluate instead of substitute. mootools has this method of the same name, however, it is directly built into the string native object. The Kissy framework uses kissy in this way. substitute (STR, O), qwrap framework after retouch, you can use '{0} love {1 }'. format ('I', 'you'), and my framework Dom. format supports two parameter passing methods:
// Http://www.cnblogs.com/rubylouvre/archive/2011/03/06/1972176.html var A = Dom. format ("result is # {0}, # {1}",); alert (a); // "result is" Var B = Dom. format ("# {name} is a # {sex}", {name: "Jhon", sex: "Man"}); alert (B ); // "Jhon is a man"
The original string operator (R/R) can easily process the backslash:
F = open (r 'C: \ Program Files \ Adobe \ reader 9.0 \ setup files \ {AC76BA86-7AD7-2052-7B44-A94000000001} \ setup. INI ', 'R') for I in F: print if. close ()
At the same time, the index and value are traversed. Previously we used for to achieve this:
For I in range (0, Len (list): print I, list [I]
Now we can use the enumerate method, which is more concise:
For index, text in enumerate (list): Print Index, text
For JavaScript, we can use the foreach iterator of javascript1.7 to process values and indexes at the same time. It directly transmits elements and indexes and arrays in the callback function.
I think the same is true for its internal implementation.
Def enumerate (Collection): 'generates an indexed series: (0, coll [0]), (1, coll [1])... 'I = 0 it = ITER (Collection) while 1: yield (I, it. next () I + = 1
| Type |
Method |
Annotation |
| Fill |
Center (width [, fillchar]),
Ljust (width [, fillchar]),
Must ust (width [, fillchar]),
Zfill (width ),
Expandtabs ([tabsize]) |
- The fillchar parameter specifies the characters to be filled. The default value is space.
- Zfill Z is short for zero. As the name suggests, it is filled with 0 characters for numerical output.
- The tabsize parameter of expandtabs () is 8 by default. Its function is to convert the tabs in a string into appropriate spaces.
|
| Delete |
Strip ([chars]),
Lstrip ([chars]),
Rstrip ([chars]) |
Chars is the character to be removed. It is a blank character by default, which is defined by the string. whitespace constant. |
| Deformation |
Lower (), # all lower case
Upper (), # all lower case
Capitalize (), # uppercase letters
Swapcase (), # case-insensitive Switching
Title () # The first and other lowercase letters of each word |
Because the title () function does not remove the blank characters at both ends of the string and does not replace the consecutive blank characters with a space,
Therefore, we recommend that you use the capwords (s) function in the string module, which can remove the blank spaces at both ends and replace the consecutive blank spaces with one space.
>>> 'Hello world! '. Title ()' Hello world! '>>> String. capwords ('Hello world! ') 'Hello world! ' |
| Cutting |
Partition (SEP ),
Rpartition (SEP ),
Splitlines ([keepends]),
Split ([Sep [, maxsplit]),
Rsplit ([Sep [, maxsplit]) |
- partition () function family is a method added in version 2.5. It accepts a string parameter and returns a tuple object with three elements.
If Sep does not appear in the parent string, the return value is (SEP, '','').
otherwise, the first element of the returned value is the left end of SEP, the second element is the SEP itself, and the third element is the right side of the SEP.
- the maxsplit parameter indicates the number of shards, that is, the maximum number of shards. Therefore, the returned value is maxsplit + 1 element.
- S. Split () and S. Split ('') return different values
>> 'Hello world! '. Split () ['hello', 'World! '] >>>' Hello world! '. Split ('') ['', '', 'Hello','', '', 'World! ']
the reason for the difference is that when the SEP parameter is ignored or the SEP parameter is none, split () two different algorithms are used.
for the former, split () removes the blank characters at both ends of the string, then, a blank string of any length is used as the separator to split the string
that is, a continuous blank string is treated as a single blank string;
for the latter, an empty string exists between two consecutive Sep. Therefore, for empty strings (or blank strings), their return values are also different:
>> ''. split () [] >>> ''. split ('') ['']
|
| Connection |
Join (SEQ) |
The efficiency of the join () function (relative to the addition of loops) makes it one of the most noteworthy string methods.
Its function is to concatenate iterated string sequences into a long string, such:
>>> Conf = {'host': '100. 0.0.1 ',... 'db': 'spam ',... 'user': 'sa ',... 'passwd': 'eggs' }>>> ';'. join ("% s = % s" % (K, V) for K, V in Conf. iteritems () 'passswd = eggs; DB = spam; user = sa; host = 127.0.0.1' |
| Determination |
Isalnum (),
Isalpha (),
Isdigit (),
Islower (),
Isupper (),
Isspace (),
Istitle (),
Startswith (prefix [, start [, end]),
Endswith (suffix [, start [, end]) |
All of these functions are simple and well-known. Note that the * With () function family can accept the optional start and end parameters to optimize the performance.
In addition, since py2.5, the prefix parameter of the * With () function family can accept real parameters of the tuple type. If one element in the real parameter can match, true is returned. |
| Search |
Count (sub [, start [, end]),
Find (sub [, start [, end]),
Index (sub [, start [, end]),
Rfind (sub [, start [, end]),
Rindex (sub [, start [, end]) |
If the find () function family cannot be found,-1 is returned. The index () function family throws a valueerror error.
Alternatively, you can use the in and not in operators to determine whether a template exists in a string. |
| Replace |
Replace (old, new [, Count]),
Translate (Table [, deletechars]) |
The Count parameter of the Replace () function is used to specify the maximum number of replace times.
The table of the translate () parameter can be generated by string. maketrans (FRM, ).
Translate () does not fully support Unicode objects. We recommend that you do not use it. |
| Encoding |
Encode ([encoding [, errors]),
Decode ([encoding [, errors]) |
This is a pair of reciprocal operations to encode and decode strings. Because STR is platform-related, the internal code it uses depends on the operating system environment,
Unicode is platform-independent and is a string storage method in Python.
Unicode can be converted into STR by encoding (encode), while STR can also be converted into Unicode by decoding (decode. |