This module contains additional features for manipulating strings.
Variables defined in the string module
include:
digitsThe string "0123456789".
lowercaseA string containing all the lowercase letters,
"abcdefghijklmnopqrstuvwxyz".
uppercaseA string containing all the uppercase letters,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ".
lettersThe concatenation of lowercase
and uppercase.
punctuationString of characters that are considered
punctuation marks:
! " # $ % & ' ( ) * + , - . / : ;
< = > ? @ [ \ ] ^ _ ` { | } ~
whitespaceCharacters considered whitespace, generally
" \t\n\r\f\v".
printableString containing all the printable characters,
the union of letters,
digits,
punctuation, and
whitespace.
octdigitsThe string "01234567".
hexdigits"0123456789abcdefABCDEF".
In addition to the variables and functions in this module, there are a large number of functions that duplicate the built-in methods on strings. For example, instead of
s.split(",", 1)you can get the same function with
import string string.split(s, ",", 1)
In general, for any built-in method
s.m(arg1,arg2,...),
you can import the string module
and get the same effect with
string.m(s,arg1,arg2,...),
Additional functions include:
maketrans(s,t)Builds a translation table to be used as the
first argument to the
string method. The
arguments S.translate() and s are two strings of
the same length; the result is a translation table
that will convert each character of
t
to the corresponding character of
s.t
Here's an example:
>>> import string
>>> bingo=string.maketrans("lLrR", "rRlL")
>>> "Cornwall Llanfair".translate(bingo)
'Colnwarr Rranfail'join(L[,d])
must be a sequence. Returns a string containing
the members of the sequence with copies of string
L
inserted between them. The default value of
d
is one space. For example,
dstring.join(['baked', 'beans, 'are',
'off']) returns the string
'baked beans are off'.