String methods were recently added to Python, introduced in version 1.6 (and in JPython 1.1), and tweaked for 2.0. These methods are intended to replace most of the functionality in the string module as well as to bring new functionality to the table. Table 6.6 shows all the current methods for strings. All string methods should fully support Unicode strings. And some are applicable only to Unicode strings.
Table 6.6. String Type Built-in Methods
Value |
Description |
string.capitalize() |
capitalizes first letter of string |
string.center(width) |
returns a space-padded string with the original string centered to a total of width columns |
string.count(str, beg= 0,end=len(string)) |
counts how many times str occurs in string, or in a substring of string if starting index beg and ending index end are given |
string.encode(encoding='UTF-8',
errors='strict')[a]
|
returns encoded string version of string; on error, default is to raise a ValueError unless errors is given with 'ignore' or 'replace'. |
string.endswith(str, beg=0,
end=len(string))[b]
|
determines if string or a substring of string (if starting index beg and ending index end are given) ends with str; returns 1 if so, and 0 otherwise |
string.expandtabs(tabsize=8) |
expands tabs in string to multiple spaces; defaults to 8 spaces per tab if tabsize not provided |
string.find(str, beg=0
end=len(string)) |
determine if str occurs in string, or in a substring of string if starting index beg and ending index end are given; returns index if found and -1 otherwise |
string.index(str, beg=0,
end=len(string)) |
same as find(), but raises an exception if str not found |
string.isa1num()[a][b][c]
|
returns 1 if string has at least 1 character and all characters are alphanumeric and 0 otherwise |
string.isalpha()[a][b][c] |
returns 1 if string has at least 1 character and all characters are alphabetic and 0 otherwise |
string.isdecimal()[b][c][d]
|
returns 1 if string contains only decimal digits and 0 otherwise |
string.isdigit()[b][c] |
returns 1 if string contains only digits and 0 otherwise |
string.islower()[b][c] |
returns 1 if string has at least 1 cased character and all cased characters are in lowercase and 0 otherwise |
string.isnumeric()[b][c][d] |
returns 1 if string contains only numeric characters and 0 otherwise |
string.isspace()[b][c] |
returns 1 if string contains only whitespace characters and 0 otherwise |
string.istitle()[b][c] |
returns 1 if string is properly "titlecased" (see title()) and 0 otherwise |
string.isupper()[b][c] |
returns 1 if string has at least one cased character and all cased characters are in uppercase and 0 otherwise |
string.join(seq) |
merges (concatenates) the string representations of elements in sequence seq into a string, with separator string |
string.ljust(width) |
returns a space-padded string with the original string left-justified to a total of width columns |
string.lower() |
converts all uppercase letters in string to lowercase |
string.lstrip() |
removes all leading whitespace in string |
string.replace(str1, str2, num=string.count(str1)) |
replaces all occurrences of str1 in string with str2, or at most num occurrences if num given |
string.rfind(str, beg=0,end=len(string)) |
same as find(), but search backwards in string |
string.rindex(
str, beg=0,
end=len(string)) |
same as index(), but search backwards in string |
string.rjust(width) |
returns a space-padded string with the original string right-justified to a total of width columns. |
string.rstrip() |
removes all trailing whitespace of string |
string.split(str="", num=string.count(str)) |
splits string according to delimiter str (space if not provided) and returns list of substrings; split into at most num substrings if given |
string.splitlines(
num=string.count('\n'))[b][c] |
splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed |
string.startswith(str, beg=0,end=len(string))[b][c] |
determines if string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns 1 if so, and 0 otherwise |
string.strip([obj]) |
performs both lstrip() and rstrip() on string |
string.swapcase() |
inverts case for all letters in string |
string.title()[b][c] |
returns "titlecased" version of string, that is, all words begin with uppercase, and the rest are lowercase (also see istitle()) |
string.translate(str, del="") |
translates string according to translation table str(256 chars), removing those in the del string |
string.upper() |
converts lowercase letters in string to uppercase |
string.zfill (width) |
returns original string left-padded with zeros to a total of width characters; intended for numbers, zfill() retains any sign given (less one zero) |