[ Team LiB ] |
9.2 The string ModuleThe string module supplies functions that duplicate each method of string objects, as covered in the previous section. Each function takes the string object as its first argument. Module string also has several useful string-valued attributes:
You should not rebind these attributes, since other parts of the Python library may rely on them and the effects of rebinding them would be undefined. 9.2.1 Locale SensitivityThe locale module is covered in Chapter 10. 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 FunctionThe method translate of plain strings, covered earlier in this chapter, takes as its first argument a plain string of length 256 that it uses as a translation table. The easiest way to build translation tables is to use the maketrans function supplied by module string.
Returns a translation table, which 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 string from is mapped to the character at the corresponding position in string onto. For each character not listed in from, the translation table maps the character to itself. To get an identity table that maps each character to itself, 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 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 |
[ Team LiB ] |