Prefix and Suffix Methods & Topological Sort
In the previous lesson, I covered the new parser and changes to the generic type hints. In this lesson, I’ll be talking about prefix and suffix removal functions added to the
str (string) class, and topological graph sorting.
In Python 3.9, strings now come with two new functions,
.removesuffix(), for pulling off something from the beginning of a string and pulling off something from the end of a string. Prior to Python 3.9, the most common way of getting rid of things from the beginning and end of a string was the
It strips off any character in that grouping—the space (
" "), the
"3", the dot (
"."), and the
"9". At the end of the string, the result that you expect is removed. At the beginning of the string, the
"3" disappears as well. This might not be what you intended.
This does what you might expect. It removes the suffix. This is actually looking for
" 3.9" and only will remove that ending. And as it’s only attacking the suffix, it leaves the
"3" at the beginning of the sentence alone.
.removeprefix() does the same thing, but at the beginning of the sentence. Notice, in this sentence, nothing’s removed, and that’s because
" 3.9" was not found at the beginning. So, unlike the
.strip() case, your
"3 cool features in Python 3.9" is left alone.
"Waikiki" still leaves one
"ki" at the end.
.removesuffix() only matches once. If you do need to remove the same suffix multiple times, put
.removesuffix() inside of a
In order to install
feedparser, you have to install
sgmllib3k. If you consider this as a dependency graph and you wanted to figure out what order you could install things in for
realpython-reader to work, you would have to install
graphlib expects you to do this as a dictionary. Each key in the dictionary represents part of the graph. The first key here,
"realpython-reader", is the root of the graph, which has two children—
This set is
feedparser’s dependency. It’s important to note that this needs to be a set. If you forget to use a set here, it will iterate over the string instead, and you’ll get weird little answers instead of what you’re actually looking for.
A couple quick things to note. First, this isn’t the only valid answer.
'html2text' could come after
'feedparser', and this would still work. When you topologically sort a graph, the result does not have to be unique. Remember earlier, when I talked about the values in the graph being sets.
Suppose for a moment that you forgot to set the
"feedparser" value as a set. Because
"sgmllib3k" is a string, and because the
TopologicalSorter iterates over the content inside of the graph, it would treat the letters
"l", et cetera, each as nodes in the graph, and you wouldn’t get the correct result.
Become a Member to join the conversation.