Next: whrandom
Prev: regsub
Up: Standard Modules
Top: Top
4.5. Standard Module string
This module defines some constants useful for checking character
classes, some exceptions, and some useful string functions.
The constants are:
- digits -- data of module string
-
The string
'0123456789'
.
- hexdigits -- data of module string
-
The string
'0123456789abcdefABCDEF'
.
- letters -- data of module string
-
The concatenation of the strings
lowercase
and
uppercase
described below.
- lowercase -- data of module string
-
A string containing all the characters that are considered lowercase
letters. On most systems this is the string
'abcdefghijklmnopqrstuvwxyz'
. Do not change its definition --
the effect on the routines upper
and swapcase
is
undefined.
- octdigits -- data of module string
-
The string
'01234567'
.
- uppercase -- data of module string
-
A string containing all the characters that are considered uppercase
letters. On most systems this is the string
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
. Do not change its definition --
the effect on the routines lower
and swapcase
is
undefined.
- whitespace -- data of module string
-
A string containing all characters that are considered whitespace.
On most systems this includes the characters space, tab, linefeed,
return, formfeed, and vertical tab. Do not change its definition --
the effect on the routines
strip
and split
is
undefined.
The exceptions are:
- atof_error -- exception of module string
-
Exception raised by
atof
when a non-float string argument is detected.
The exception argument is the offending string.
- atoi_error -- exception of module string
-
Exception raised by
atoi
when a non-integer string argument is detected.
The exception argument is the offending string.
- atol_error -- exception of module string
-
Exception raised by
atol
when a non-integer string argument is detected.
The exception argument is the offending string.
- index_error -- exception of module string
-
Exception raised by
index
when sub is not found.
The exception argument is undefined (it may be a tuple containing the
offending arguments to index
or it may be the constant string
'substring not found'
).
The functions are:
- atof (s) -- function of module string
-
Convert a string to a floating point number. The string must have
the standard syntax for a floating point literal in Python, optionally
preceded by a sign (`+' or `-').
- atoi (s) -- function of module string
-
Convert a string to an integer. The string must consist of one or more
digits, optionally preceded by a sign (`+' or `-').
- atol (s) -- function of module string
-
Convert a string to a long integer. The string must consist of one
or more digits, optionally preceded by a sign (`+' or `-').
- expandtabs (s, tabsize) -- function of module string
-
Expand tabs in a string, i.e. replace them by one or more spaces,
depending on the current column and the given tab size. The column
number is reset to zero after each newline occurring in the string.
This doesn't understand other non-printing characters or escape
sequences.
- find (s, sub, i) -- function of module string
-
Return the lowest index in s not smaller than i where the
substring sub is found. Return
-1
when sub
does not occur as a substring of s with index at least i.
If i is omitted, it defaults to 0
. If i is
negative, len(s)
is added.
- rfind (s, sub, i) -- function of module string
-
Like
find
but finds the highest index.
- index (s, sub, i) -- function of module string
-
Like
find
but raise index_error
when the substring is
not found.
- rindex (s, sub, i) -- function of module string
-
Like
rfind
but raise index_error
when the substring is
not found.
- lower (s) -- function of module string
-
Convert letters to lower case.
- split (s) -- function of module string
-
Returns a list of the whitespace-delimited words of the string
s.
- splitfields (s, sep) -- function of module string
-
Returns a list containing the fields of the string s, using
the string sep as a separator. The list will have one more
items than the number of non-overlapping occurrences of the
separator in the string. Thus,
string.splitfields(s, '
')
is not the same as string.split(s)
, as the latter
only returns non-empty words. As a special case,
splitfields(s, '')
returns [s]
, for any string
s. (See also regsub.split()
.)
- join (words) -- function of module string
-
Concatenate a list or tuple of words with intervening spaces.
- joinfields (words, sep) -- function of module string
-
Concatenate a list or tuple of words with intervening separators.
It is always true that
string.joinfields(string.splitfields(t, sep), sep)
equals t.
- strip (s) -- function of module string
-
Removes leading and trailing whitespace from the string
s.
- swapcase (s) -- function of module string
-
Converts lower case letters to upper case and vice versa.
- upper (s) -- function of module string
-
Convert letters to upper case.
- ljust (s, width) -- function of module string
-
- rjust (s, width) -- function of module string
-
- center (s, width) -- function of module string
-
These functions respectively left-justify, right-justify and center a
string in a field of given width.
They return a string that is at least
width
characters wide, created by padding the string
s
with spaces until the given width on the right, left or both sides.
The string is never truncated.
- zfill (s, width) -- function of module string
-
Pad a numeric string on the left with zero digits until the given
width is reached. Strings starting with a sign are handled correctly.