module includes a variety of text-processing utilities that go above
and beyond string expression operators. For instance:
string.find performs substring
string.atoi converts strings to
string.strip removes leading and trailing
string.upper converts to
string.replace performs substring
The Python library manual includes an exhaustive list of available
tools. Moreover, as of Python 2.0, Unicode (wide) strings are fully
supported by Python string tools, and most of the
string module’s functions are also now
available as string object methods. For
instance, in Python 2.0, the following two expressions are
string.find(str, substr) # traditional str.find(substr) # new in 2.0
except that the second form does not require callers to import the
string module first. As usual, you should consult
the library manuals and Appendix A, for
late-breaking news on the string tools front.
In terms of this chapter’s main focus, though, Python’s built-in tools for splitting and joining strings around tokens turn out to be especially useful when it comes to parsing text:
Splits a string into substrings, using either whitespace (tabs, spaces, newlines) or an explicitly passed string as a delimiter.
Concatenates a list or tuple of substrings, adding a space or an explicitly passed separator string between each.
As we saw
earlier in this book,
split chops a string ...