Character 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. One of these, string.digits is equivalent to “0123456789”. It can be used to check if a character is a digit using the in operator.

The string string.ascii_lowercase contains all of the ascii letters that the system considers to be lowercase. Similarly, string.ascii_uppercase contains all of the uppercase letters. string.punctuation comprises all the characters considered to be punctuation. You can’t actually run the code below in the browser (sorry, limitation of our environment, not all of python has been implemented.) But the comments indicate what would be produced; later in the semester you’ll have facilities for actually executing it.

For more information consult the string module documentation (see Global Module Index).

Note

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.

Next Section - Lists