String Methods

Return to Python Pocket Reference Table of Contents

String methods

In addition to the format() method described earlier, string method calls provide higher-level text processing tools beyond string expressions. Table 9 lists available string method calls; in this table, S is any string object (technically, a 3.X str). String methods that modify text always return a new string and never modify the object in-place (strings are immutable).

For more details on methods in the table, see the functional area description sections ahead, or run a help(str.method) interactively. Hint: this list can vary across Python releases; to see yours, try:

sorted(x for x in dir(str) if not x.startswith('__'))

See also the re module in The re Pattern-Matching Module for pattern-based equivalents to some string type methods.

Table 9. Python 3.X string method calls

S.capitalize()

S.casefold() (as of Python 3.3)

S.center(width, [, fill])

S.count(sub [, start [, end)

S.encode(encoding [, errors]])

S.endswith(suffix [, start [, end)

S.expandtabs(tabsize)

S.find(sub [, start [, end)

S.format(*args, **kwargs)

S.format_map(mapping) (as of Python 3.2)

S.index(sub [, start [, end)

S.isalnum()

S.isalpha()

S.isdecimal()

S.isdigit()

Fair Use Source: B00HZ41PGC