String Type Built-in Methods


The descriptions for the string built-in methods listed above are given in Table B.7.

Table B.7. String Type Built-in Methods (continued)

Method Name

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.decode(encoding='UTF-8', errors='strict') [** ed. add FN: New in Python 2.2]

Returns decoded string version of string; on error, default is to raise a ValueError unless errors is given with 'ignore' or 'replace'

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 true if so, and False 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))

Determines 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.isalnum()[a],[b],[c]

Returns true if string has at least 1 character and all characters are alphanumeric and False otherwise

string.isalpha()[a],[b],[c]

Returns TRue if string has at least 1 character and all characters are alphabetic and False otherwise

string.isdecimal()[b],[c],[d]

Returns TRue if string contains only decimal digits and False otherwise

string.isdigit()[b],[c]

Returns true if string contains only digits and False otherwise

string.islower()[b],[c]

Returns true if string has at least 1 cased character and all cased characters are in lowercase and False otherwise

string.isnumeric()[b],[c][d]

Returns true if string contains only numeric characters and False otherwise

string.isspace()[b],[c]

Returns true if string contains only whitespace characters and False otherwise

string.istitle()[b],[c]

Returns true if string is properly "titlecased" (see title()) and False otherwise

string.isupper()[b],[c]

Returns TRue if string has at least one cased character and all cased characters are in uppercase and False 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]

Determines if string or a substring of string (if starting index beg and ending index end are given) starts with substring str; returns TRue if so, and False 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)


[a] Applicable to Unicode strings only in 1.6, but to all string types in 2.0.

[b] Not available as a string module function in 1.5.2.

[c] New in Python 2.1.

[d] Applicable to Unicode strings only.



Core Python Programming
Core Python Programming (2nd Edition)
ISBN: 0132269937
EAN: 2147483647
Year: 2004
Pages: 334
Authors: Wesley J Chun

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net