next up previous contents index
Next: top Up: Strings Previous: top   Contents   Index

startsection section 1 0mm-3.5ex plus -1ex minus -.2ex0.7ex plus.2exCharacter classification

It is often helpful to examine a character and test whether it is upper- or lowercase, or whether it is a character or a digit. The string module provides several constants that are useful for these purposes.

The string string.lowercase contains all of the letters that the system considers to be lowercase. Similarly, string.uppercase contains all of the uppercase letters. Try the following and see what you get:


>>> print string.lowercase
>>> print string.uppercase
>>> print string.digits

We can use these constants and find to classify characters. For example, if find(lowercase, ch) returns a value other than -1, then ch must be lowercase:


def isLower(ch):
  return string.find(string.lowercase, ch) != -1

Alternatively, we can take advantage of the in operator, which determines whether a character appears in a string:


def isLower(ch):
  return ch in string.lowercase

As yet another alternative, we can use the comparison operator:


def isLower(ch):
  return 'a' <= ch <= 'z'

If ch is between a and z, it must be a lowercase letter.

As an exercise, discuss which version of isLower you think will be fastest. Can you think of other reasons besides speed to prefer one or the other?

Another constant defined in the string module may surprise you when you print it:


>>> print string.whitespace

Whitespace characters move the cursor without printing anything. They create the white space between visible characters (at least on white paper). The constant string.whitespace contains all the whitespace characters, including space, tab (\t), and newline (\n).

There are other useful functions in the string module, but this book isn't intended to be a reference manual. On the other hand, the Python Library Reference is. Along with a wealth of other documentation, it's available from the Python website, www.python.org.


next up previous contents index
Next: top Up: Strings Previous: top   Contents   Index
root 2004-05-05