Next: 4.2 Built-in Module regex
Up: 4 String Services
Previous: 4 String Services
This module defines some constants useful for checking character
classes and some useful string functions. See the modules
regex
and regsub
for string functions based on regular
expressions.
The constants defined in this module are are:
- digits
-
The string
'0123456789'
.
- hexdigits
-
The string
'0123456789abcdefABCDEF'
.
- letters
-
The concatenation of the strings
lowercase
and
uppercase
described below.
- lowercase
-
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
-
The string
'01234567'
.
- uppercase
-
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
-
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 functions defined in this module are:
- atof(s)
-
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[, base])
-
Convert string s to an integer in the given base. The
string must consist of one or more digits, optionally preceded by a
sign (`+' or `-'). The base defaults to 10. If it is
0, a default base is chosen depending on the leading characters of the
string (after stripping the sign): `0x' or `0X' means 16,
`0' means 8, anything else means 10. If base is 16, a
leading `0x' or `0X' is always accepted. (Note: for a more
flexible interpretation of numeric literals, use the built-in function
eval()
.)
- atol(s[, base])
-
Convert string s to a long integer in the given base. The
string must consist of one or more digits, optionally preceded by a
sign (`+' or `-'). The base argument has the same
meaning as for
atoi()
. A trailing `l' or `L' is not
allowed, except if the base is 0.
- capitalize(word)
-
Capitalize the first character of the argument.
- capwords(s)
-
Split the argument into words using
split
, capitalize each word
using capitalize
, and join the capitalized words using
join
. Note that this replaces runs of whitespace characters by
a single space. (See also regsub.capwords()
for a version
that doesn't change the delimiters, and lets you specify a word
separator.)
- expandtabs(s, tabsize)
-
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[, start])
-
Return the lowest index in s not smaller than start where the
substring sub is found. Return
-1
when sub
does not occur as a substring of s with index at least start.
If start is omitted, it defaults to 0
. If start is
negative, len(s)
is added.
- rfind(s, sub[, start])
-
Like
find
but find the highest index.
- index(s, sub[, start])
-
Like
find
but raise ValueError
when the substring is
not found.
- rindex(s, sub[, start])
-
Like
rfind
but raise ValueError
when the substring is
not found.
- count(s, sub[, start])
-
Return the number of (non-overlapping) occurrences of substring
sub in string s with index at least start.
If start is omitted, it defaults to
0
. If start is
negative, len(s)
is added.
- lower(s)
-
Convert letters to lower case.
- maketrans(from, to)
-
Return a translation table suitable for passing to
string.translate
or regex.compile
, that will map each character in from
into the character at the same position in to; from and
to must have the same length.
- split(s[, sep[, maxsplit]])
-
Return a list of the words of the string s. If the optional
second argument sep is absent or
None
, the words are
separated by arbitrary strings of whitespace characters (space, tab,
newline, return, formfeed). If the second argument sep is
present and not None
, it specifies a string to be used as the
word separator. The returned list will then have one more items than
the number of non-overlapping occurrences of the separator in the
string. The optional third argument maxsplit defaults to 0. If
it is nonzero, at most maxsplit number of splits occur, and the
remainder of the string is returned as the final element of the list
(thus, the list will have at most maxsplit+1
elements).
(See also regsub.split()
for a version that allows specifying a
regular expression as the separator.)
- splitfields(s[, sep[, maxsplit]])
-
This function behaves identical to
split
. (In the past,
split
was only used with one argument, while splitfields
was only used with two arguments.)
- join(words[, sep])
-
Concatenate a list or tuple of words with intervening occurrences of
sep. The default value for sep is a single space character.
It is always true that
string.join(string.split(s, sep), sep)
equals s.
- joinfields(words[, sep])
-
This function behaves identical to
join
. (In the past,
join
was only used with one argument, while joinfields
was only used with two arguments.)
- lstrip(s)
-
Remove leading whitespace from the string s.
- rstrip(s)
-
Remove trailing whitespace from the string s.
- strip(s)
-
Remove leading and trailing whitespace from the string s.
- swapcase(s)
-
Convert lower case letters to upper case and vice versa.
- translate(s, table[, deletechars])
-
Delete all characters from s that are in deletechars (if present), and
then translate the characters using table, which must be
a 256-character string giving the translation for each character
value, indexed by its ordinal.
- upper(s)
-
Convert letters to upper case.
- ljust(s, width)
-
,rjusts, width
,centers, width
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)
-
Pad a numeric string on the left with zero digits until the given
width is reached. Strings starting with a sign are handled correctly.
This module is implemented in Python. Much of its functionality has
been reimplemented in the built-in module strop
. However, you
should never import the latter module directly. When
string
discovers that strop
exists, it transparently
replaces parts of itself with the implementation from strop
.
After initialization, there is no overhead in using
string
instead of strop
.
Next: 4.2 Built-in Module regex
Up: 4 String Services
Previous: 4 String Services
guido@cnri.reston.va.us