Skip to Main Content
Programming Python, 3rd Edition
book

Programming Python, 3rd Edition

by Mark Lutz
August 2006
Intermediate to advanced content levelIntermediate to advanced
1600 pages
51h 46m
English
O'Reilly Media, Inc.
Content preview from Programming Python, 3rd Edition

String Method Utilities

Python’s string methods include a variety of text-processing utilities that go above and beyond string expression operators. For instance, given an instance str of the built-in string object type:

str.find(substr)

Performs substring searches

str.replace(old, new)

Performs substring substitutions

str.split(delim)

Chops up a string around delimiters

str.join(seq)

Puts substrings together with delimiters between

str.strip( )

Removes leading and trailing whitespace

str.rstrip( )

Removes trailing whitespace only, if any

str.rjust(width)

Right-justifies a string in a fixed-width field

str.upper( )

Converts to uppercase

str.isupper( )

Tests whether the string is uppercase

str.isdigit( )

Tests whether the string is all digit characters

str.endswith(substr)

Tests for a substring at the end

str.startswith(substr)

Tests for a substring at the front

This list is representative but partial, and some of these methods take additional optional arguments. For the full list of string methods, run a dir(str) call at the Python interactive prompt and run help(str.method) on any method for some quick documentation. The Python library manual also includes an exhaustive list.

Moreover, in Python today, Unicode (wide) strings fully support all normal string methods, and most of the older string module’s functions are also now available as string object methods. For instance, in Python 2.0 and later, the following two expressions are equivalent:

string.find(aString, substr) # original module aString.find(substr) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Python, 3rd Edition

Learning Python, 3rd Edition

Mark Lutz

Publisher Resources

ISBN: 0596009259Supplemental ContentErrata Page