7.10. The string Module

We’ve talked before about string methods. The Python language gives us another means of working with string data. The Python string module provides several constants that are useful for checking to see if a character, slice, or string contains letters, digits, symbols, etc. To use these constants, we need to import the module into our code.

Begin with an import statement at the top of your code:

1
import string

7.10.1. Using string Constants

Let’s say we want to check if one character from a string is a digit or a lowercase letter. We could set up a conditional like so:

1
2
3
4
5
6
if char in '0123456789':
   # Code for the digit case...
elif char in 'abcdefghijklmnopqrstuvwxyz':
   # Code for the lowercase letter case...
else:
   # Alternate code here...

Note what we did:

  1. Line 1 checks if the character is any of the digits 0 - 9,

  2. If not, line 3 checks if the character is any of the lowercase letters from a - z.

  3. We could easily expand the conditional to check for uppercase letters (elif char in 'ABC...'), punctuation, etc.

Although fairly straightforward, setting up the checks is tedious, since we need to hard-code the strings for the numbers, letters, and symbols. (Go ahead and type out the lowercase letters, then the uppercase letters, and then check for any mistakes. How long did that take?)

Fortunately, Python has already defined the more common character combinations. For example, string.digits is equivalent to '0123456789', and string.ascii_lowercase is the same as 'abcdefghijklmnopqrstuvwxyz'.

The table below summarizes several of the constants defined in the string module. For a complete list, refer to the Python documentation.

Common String Methods

Syntax

Description

string.digits

Returns the string '0123456789'

string.ascii_lowercase

Returns the string 'abcdefghijklmnopqrstuvwxyz'

string.ascii_uppercase

Returns the string 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

string.ascii_letters

Returns a string containing all of the uppercase and lowercase letters.

string.punctuation

Returns the string '!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~'.

7.10.1.1. Try It!

Run the following code to see the values assigned to each string module constant:

Now try this:

  1. Add elif blocks to the code to check if my_string is an uppercase letter, a digit, or some type of punctuation. Your code should print a different message depending on the value of my_string.

  2. Add an else statement to deal with characters that are not letters, digits, or punctuation.

  3. Assign a longer string to my_string. Add a loop to your code so that it checks each character in my_string and reports the results.

  4. Note that whitespace characters are not included in any of these constants. Can you guess the name for the string module constant that contains ' ', '\t', and '\n'?

Note

Python contains a number of methods that return True or False based on the characteristics of an entire string instead of a single character. These can be used as alternatives to the examples above.

For example, the isdigit() method returns True if ALL the characters in a string are digits. So '21'.isdigit() returns True while '21' in string.digits returns False.

Check out W3Schools for more details about these methods.

7.10.2. Check Your Understanding

Question

Which of the following expressions evaluate to True? Click each option to check your prediction.

  1. 'a' in string.ascii_uppercase
  2. 'Q' in string.ascii_letters
  3. '334' in string.digits
  4. ' ' in string.punctuation
  5. '$' in string.punctuation
  6. 'abc' in 'abcdefghijklmnopqrstuvwxyz'
  7. 'eo' in 'aeiou'