📄 chap2.txt
字号:
=================================================================
MODULE -- string : A collection of string operations
=================================================================
There are a number of general things to notice about the
functions in the [string] module (which is composed entirely
of functions and constants; no classes).
1. Strings are immutable (as discussed in Chapter 1). This
means that there is no such thing as changing a string "in
place" (as we might do in many other languages, such as C,
by changing the bytes at certain offsets within the
string). Whenever a [string] module function takes a
string object as an argument, it returns a brand-new
string object and leaves the original one as is. However,
the very common pattern of binding the same name on the
left of an assignment as was passed on the right side
within the [string] module function somewhat conceals this
fact. For example:
>>> import string
>>> str = "Mary had a little lamb"
>>> str = string.replace(str, 'had', 'ate')
>>> str
'Mary ate a little lamb'
The first string object never gets modified per se; but
since the first string object is no longer bound to any
name after the example runs, the object is subject to
garbage collection and will disappear from memory. In
short, calling a [string] module function will not change
any existing strings, but rebinding a name can make it
look like they changed.
2. Many [string] module functions are now also available as
string object methods. To use these string object
methods, there is no need to import the [string] module,
and the expression is usually slightly more concise.
Moreover, using a string object method is usually slightly
faster than the corresponding [string] module function.
However, the most thorough documentation of each
function/method that exists as both a [string] module
function and a string object method is contained in this
reference to the [string] module.
3. The form 'string.join(string.split(...))' is a frequent
Python idiom. A more thorough discussion is contained in
the reference items for `string.join()` and
`string.split()`, but in general, combining these two
functions is very often a useful way of breaking down a
text, processing the parts, then putting together the
pieces.
4. Think about clever `string.replace()` patterns. By
combining multiple `string.replace()` calls with use of
"place holder" string patterns, a surprising range of
results can be achieved (especially when also manipulating
the intermediate strings with other techniques). See the
reference item for `string.replace()` for some discussion
and examples.
5. A mutable string of sorts can be obtained by using built-in
lists, or the [array] module. Lists can contain a
collection of substrings, each one of which may be replaced
or modified individually. The [array] module can define
arrays of individual characters, each position modifiable,
included with slice notation. The function `string.join()`
or the method `"".join()` may be used to re-create true
strings; for example:
>>> lst = ['spam','and','eggs']
>>> lst[2] = 'toast'
>>> print ''.join(lst)
spamandtoast
>>> print ' '.join(lst)
spam and toast
Or:
>>> import array
>>> a = array.array('c','spam and eggs')
>>> print ''.join(a)
spam and eggs
>>> a[0] = 'S'
>>> print ''.join(a)
Spam and eggs
>>> a[-4:] = array.array('c','toast')
>>> print ''.join(a)
Spam and toast
CONSTANTS:
The [string] module contains constants for a number of frequently
used collections of characters. Each of these constants is itself
simply a string (rather than a list, tuple, or other collection).
As such, it is easy to define constants alongside those provided
by the [string] module, should you need them. For example:
>>> import string
>>> string.brackets = "[]{}()<>"
>>> print string.brackets
[]{}()<>
string.digits
The decimal numerals ("0123456789").
string.hexdigits
The hexadecimal numerals ("0123456789abcdefABCDEF").
string.octdigits
The octal numerals ("01234567").
string.lowercase
The lowercase letters; can vary by language. In English
versions of Python (most systems):
>>> import string
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
You should not modify `string.lowercase` for a source
text language, but rather define a new attribute, such as
'string.spanish_lowercase' with an appropriate string
(some methods depend on this constant).
string.uppercase
The uppercase letters; can vary by language. In English
versions of Python (most systems):
>>> import string
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
You should not modify `string.uppercase` for a source
text language, but rather define a new attribute, such as
'string.spanish_uppercase' with an appropriate string
(some methods depend on this constant).
string.letters
All the letters (string.lowercase+string.uppercase).
string.punctuation
The characters normally considered as punctuation; can
vary by language. In English versions of Python (most
systems):
>>> import string
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
string.whitespace
The "empty" characters. Normally these consist of tab,
linefeed, vertical tab, formfeed, carriage return, and
space (in that order):
>>> import string
>>> string.whitespace
'\011\012\013\014\015 '
You should not modify `string.whitespace` (some methods
depend on this constant).
string.printable
All the characters that can be printed to any device; can
vary by language
(string.digits+string.letters+string.punctuation+string.whitespace)
FUNCTIONS:
string.atof(s=...)
Deprecated. Use `float()`.
Converts a string to a floating point value.
SEE ALSO, `eval()`, `float()`
string.atoi(s=... [,base=10])
Deprecated with Python 2.0. Use `int()` if no custom
base is needed or if using Python 2.0+.
Converts a string to an integer value (if the string
should be assumed to be in a base other than 10, the base
may be specified as the second argument).
SEE ALSO, `eval()`, `int()`, `long()`
string.atol(s=... [,base=10])
Deprecated with Python 2.0. Use `long()` if no custom
base is needed or if using Python 2.0+.
Converts a string to an unlimited length integer value
(if the string should be assumed to be in a base other
than 10, the base may be specified as the second argument).
SEE ALSO, `eval()`, `long()`, `int()`
string.capitalize(s=...)
"".capitalize()
Return a string consisting of the initial character
converted to uppercase (if applicable), and all other
characters converted to lowercase (if applicable):
>>> import string
>>> string.capitalize("mary had a little lamb!")
'Mary had a little lamb!'
>>> string.capitalize("Mary had a Little Lamb!")
'Mary had a little lamb!'
>>> string.capitalize("2 Lambs had Mary!")
'2 lambs had mary!'
For Python 1.6+, use of a string object method is
marginally faster and is stylistically preferred in most
cases:
>>> "mary had a little lamb".capitalize()
'Mary had a little lamb'
SEE ALSO, `string.capwords()`, `string.lower()`
string.capwords(s=...)
"".title()
Return a string consisting of the capitalized words.
An equivalent expression is:
#*----- equivalent expression -----#
string.join(map(string.capitalize,string.split(s))
But `string.capwords()` is a clearer way of writing it. An
effect of this implementation is that whitespace is
"normalized" by the process:
>>> import string
>>> string.capwords("mary HAD a little lamb!")
'Mary Had A Little Lamb!'
>>> string.capwords("Mary had a Little Lamb!")
'Mary Had A Little Lamb!'
With the creation of string methods in Python 1.6, the
module function `string.capwords()` was renamed as a string
method to `"".title()`.
SEE ALSO, `string.capitalize()`, `string.lower()`,
`"".istitle()`
string.center(s=..., width=...)
"".center(width)
Return a string with 's' padded with symmetrical leading
and trailing spaces (but not truncated) to occupy length
'width' (or more).
>>> import string
>>> string.center(width=30,s="Mary had a little lamb")
' Mary had a little lamb '
>>> string.center("Mary had a little lamb", 5)
'Mary had a little lamb'
For Python 1.6+, use of a string object method is
stylistically preferred in many cases:
>>> "Mary had a little lamb".center(25)
' Mary had a little lamb '
SEE ALSO, `string.ljust()`, `string.rjust()`
string.count(s, sub [,start [,end]])
"".count(sub [,start [,end]])
Return the number of nonoverlapping occurrences of 'sub'
in 's'. If the optional third or fourth arguments are
specified only the corresponding slice of 's' is
examined.
>>> import string
>>> string.count("mary had a little lamb", "a")
4
>>> string.count("mary had a little lamb", "a", 3, 10)
2
For Python 1.6+, use of a string object method is
stylistically preferred in many cases:
>>> 'mary had a little lamb'.count("a")
4
"".endswith(suffix [,start [,end]])
This string method does not have an equivalent in the
[string] module. Return a Boolean value indicating whether
the string ends with the suffix 'suffix'. If the optional
second argument 'start' is specified, only consider the
terminal substring after offset 'start'. If the optional
third argument 'end' is given, only consider the slice
'[start:end]'.
SEE ALSO, `"".startswith()`, `string.find()`
string.expandtabs(s=... [,tabsize=8])
"".expandtabs([,tabsize=8])
Return a string with tabs replaced by a variable number
of spaces. The replacement causes text blocks to line up
at "tab stops." If no second argument is given, the new
string will line up at multiples of 8 spaces. A newline
implies a new set of tab stops.
>>> import string
>>> s = 'mary\011had a little lamb'
>>> print s
mary had a little lamb
>>> string.expandtabs(s, 16)
'mary had a little lamb'
>>> string.expandtabs(tabsize=1, s=s)
'mary had a little lamb'
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -