Previous Page
Next Page

9.2. The string Module

The string module supplies functions that duplicate each method of string objects, as covered in "Methods of String Objects" on page 186. Each function takes the (plain or Unicode) string object as its first argument. Module string also supplies several useful plain-string attributes:


ascii_letters

The string ascii_lowercase+ascii_uppercase


ascii_lowercase

The string 'abcdefghijklmnopqrstuvwxyz'


ascii_uppercase

The string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


digits

The string '0123456789'


hexdigits

The string '0123456789abcdefABCDEF'


letters

The string lowercase+uppercase


lowercase

A string containing all characters that are deemed lowercase letters: at least 'abcdefghijklmnopqrstuvwxyz', but more letters (e.g., accented ones) may be present, depending on the active locale


octdigits

The string '01234567'


punctuation

The string '!"#$%&\'( )*+,-./:;<=>?@[\\]^_'{|}~' (i.e., all ASCII characters that are deemed punctuation characters in the 'C' locale; does not depend on which locale is active)


printable

The string of those characters that are deemed printable (i.e., digits, letters, punctuation, and whitespace)


uppercase

A string containing all characters that are deemed uppercase letters: at least 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', but more letters (e.g., accented ones) may be present, depending on the active locale


whitespace

A string containing all characters that are deemed whitespace: at least space, tab, linefeed, and carriage return, but more characters (e.g., certain control characters) may be present, depending on the active locale

You should not rebind these attributes, since other parts of the Python library may rely on them and the effects of rebinding them are undefined.

Module string also supplies class Template, covered in "Template Strings" on page 196.

9.2.1. Locale Sensitivity

The locale module is covered in "The locale Module" on page 269. Locale setting affects some attributes of module string (letters, lowercase, uppercase, whitespace). Through these attributes, locale setting also affects functions of module string and methods of plain-string objects that deal with classification of characters as letters, and conversion between upper- and lowercase, such as capitalize, isalnum, and isalpha. The corresponding methods of Unicode strings are not affected by locale setting.

9.2.2. The maketrans Function

The method translate of plain strings, covered in TRanslate on page 190, takes as its first argument a plain string of length 256 to use as a translation table. The easiest way to build translation tables is to use the maketrans function supplied by module string.

maketrans

maketrans(from,onto)

Returns a translation tablethat is, a plain string of length 256 that provides a mapping from characters in ascending ASCII order to another set of characters. from and onto must be plain strings, with len(from) equal to len(onto). Each character in from is mapped to the character at the corresponding position in onto. Each character not listed in from is mapped to itself. To get an "identity" table, call maketrans('','').

With the translate string method, you can delete characters as well as translate them. When you use TRanslate just to delete characters, the first argument you pass to translate should be the identity table. Here's an example of using the maketrans function and the string method translate to delete vowels:

import string identity = string.maketrans('','')
print 'some string'.translate(identity,'aeiou')
# prints: sm strng

The Unicode equivalent of this would be:

no_vowels = dict.fromkeys(ord(x) for x in 'aeiou')
print u'some string'.translate(no_vowels)    # prints: sm strng

Here are examples of turning all other vowels into a's and also deleting s's:

intoas = string.maketrans('eiou','aaaa')
print 'some string'.translate(intoas)              # prints: sama strang print 'some string'.translate(intoas,'s')          # prints: ama trang

The Unicode equivalent of this would be:

intoas = dict.fromkeys((ord(x) for x in 'eiou'), 'a')
print u'some string'.translate(intoas)             # prints: sama strang intoas_nos = dict(intoas, s='None')
print u'some string'.translate(intoas_nos)         # prints: ama trang



Previous Page
Next Page