In this lesson, you’ll learn about the
string module. This module will help you quickly access some string constants. Here’s an example:
>>> import string >>> string.digits '0123456789' >>> string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz'
Here are some useful constants:
You can check out the Python documentation on the
In this section, you’ll learn all about Python’s standard library. Let’s start with the
string module. The
string module is really helpful when you have any interview question that deals with strings. Let’s first start by talking about ASCII code.
It’s also how you compare strings, because something like
'abc' > 'abd'—it would compare the ASCII code of the first character, then the ASCII code of the second, and then the third, to see if they’re different.
'HELLO WORLD'.isupper(). So, this is a built-in method in
string, which will check if the whole string is uppercase. Now, let’s look at the
string module to see what are the uppercase characters.
import string and then
string.ascii_uppercase. This will spit out a string that is all the uppercase ASCII characters. Notice how space (
' ') is not in there, but
isupper() actually does return
Let’s write a better
is_upper() method—or, I guess not better, but one that takes into account if the string has spaces. So,
is_upper(), do something like
for letter in s:
if letter not in string.ascii_uppercase:—so, this means that the letter we’re on is not uppercase, then you know for a fact that we can return
False, because the string is not uppercase.
Then, if you get through all the letters and none of them check this
if statement, you can return
True. So now, calling
'HELLO WORLD' will return
False because there’s a whitespace, but if you remove that, it will return
True. There’s one small optimization you can do here.
string.ascii_uppercase has 26 letters, and this line
if letter not in […] actually checks membership, so it might potentially have to loop through the entire string to find if the letter is not in that string.
Let’s just call this a different function—
is_upper_using_set(). Let’s do a quick
%timeit (time it).
%timeit is a magic method that is built into IPython, where you pass it a expression and it will evaluate that a bunch of times, then find the mean and the standard deviation for how long it took.
Then, let’s run our
400 nanoseconds. So, you save about 170 nanoseconds each time, which really isn’t that significant, but if you’re running this, you know, millions of times, it might add up. And, it’s just good practice when looking for membership to use sets. Okay, let’s rewrite this function a little bit cleaner—
We’ll still use the set, but we’ll also do it in one line using the
all() syntax. Remember,
all() is a function that takes in an iterable and returns
True if all of the values are
'HELLO WORLD' returns
False, and without the space, returns
True. You might be thinking, “This short circuits, but doesn’t this evaluate each time?” Well, that’s not the case here, because
letter in uppercase_set for letter in s is actually a generator, so
all() will call
next() on each value and short circuit if it sees a
string.hexdigits will give you all the possible digits when looking at hexadecimal numbers,
string.octdigits will give you all the digits used in octal decimal numbers,
string.punctuation will give you all the different types of punctuation,
string.printable will give you all the possible characters that are considered printable, then
string.whitespace is all the possible whitespaces.
Let’s just look quickly at an example of removing all the types of whitespaces from a string. First, you could create the
whitespace_set and then create a generator expression
letter for letter in s—just like,
'HELLO WORLD', or something.
like this. Just a clean one-line, or you could obviously extract it into a
for loop and
.append() to a new string. This was a short video on the
string module. In an interview, if you have to somehow compare characters to some of these
string constants, instead of defining your own constants, you can use the built-in ones. In the next video, you’ll learn about the
itertools module, which has a lot of functions that return iterators for efficient looping.
Become a Member to join the conversation.