This section describes string, list and dictionary methods which can be used in scripts.
Table 2.4. List of string methods. See String Methods for a complete reference.
Method name | Syntax | Description |
---|---|---|
capitalize | capitalize() | Returns a copy of the string with the first character converted to uppercase |
center | center(width) | Returns a string of length width which contains a centered copy of the original string. Padding is done with spaces. If widht is less than the length of the original string, the original string is returned. |
count | count(sub[, start[, end]]) | Returns the number of occurences of the string sub in the original string. Scope may be limited to the substring delimited by start and end. |
encode | encode([encoding[,errors]]) | Returns an encoded version of the string |
endswith | endswith(suffix[, start[, end]]) | Returns true if the string ends with suffix. Scope may be limited to the substring delimited by start and end. |
expandtabs | expandtabs([tabsize]) | Returns a copy of the string in which every tab character is replaced by tabsize spaces. By default tabsize - 8. |
find | find(sub[, start[, end]]) | Returns the lowest index in the string at which the substring sub begins. Returns -1 if sub is not found. Scope may be limited to the substring delimited by start and end. |
index | index(sub[, start[, end]]) | Similar to find but raises ValueError when the substring is not found. |
isalnum | isalnum() | Returns true if the string is non-empty and all characters are alphanumeric. False otherwise. |
isalpha | isalpha() | Returns true if the string is non-empty and all characters are alphabetical. False otherwise. |
isdigit | isdigit() | Returns true if the string is non-empty and contains only numeric cgharacters (digits). False otherwise. |
islower | islower() | Returns true if the string is non-empty and all cased characters in it are lowercase. False otherwise. |
isspace | isspace() | Returns true if the string is non-empty and contains only whitespace characters. False otherwise. |
istitle | istitle() | Returns true if the string is non-empty and is titlecased, i.e. uppercase characters follow only uncased characters and lowercase characters follow only uppercase ones. False otherwise. |
isupper | isupper() | Returns true if the string is non-empty and all cased characters are uppercase. False otherwise. |
join | join(seq) | Returns a string which is a concatenation of the strings in the sequence seq separated by the original string. |
ljust | ljust(width) | Returns the string of length width in which the original string is left-justified. Padding is done with spaces. If widht is less than the length of the original string, the original string is returned. |
lower | lower() | Returns a copy of the original string with all the cased characters converted to lowercase. |
lstrip | lstrip() | Returns a copy of the string with leading whitespace characters removed. |
replace | replace(old, new[, maxsplit]) | Returns a copy of the string with the first maxsplitoccurrences of old replaced by new. If maxsplit is omitted, all occurrences are replaced. |
rfind | rfind(sub [,start [,end]]) | Returns the highest index in the string at which the substring sub is found. Scope may be limited to the substring delimited by start and end. |
rindex | rindex(sub[, start[, end]]) | Similar to rfind but raises ErrorValue when the substring sub is not found. |
rjust | rjust(width) | Returns the string of length width in which the original string is right-justified. Padding is done with spaces. If widht is less than the length of the original string, the original string is returned. |
rstrip | rstrip() | Returns a copy of the string with trailing whitespace characters removed. |
split | split([sep [,maxsplit]]) | Returns a list of the first maxsplit substrings in the original string delimited by sep. If sep is omitted any whitespace character is considered as the separator. If maxsplit is omitted, all substrings are returned. |
splitlines | splitlines([keepends]) | Returns a list of lines in the string, breaking at line boundaries. Linebreaks are not included in the resulting strings unless keepends is included and equal to true. |
startswith | startswith(prefix[, start[, end]]) | Returns true if the string starts with prefix, false otherwise. Scope may be limited to the substring delimited by start and end. |
strip | strip() | Returns a copy of the string with leading and trailing whitespace characters removed. |
swapcase | swapcase() | Returns a copy of the string with all uppercase characters converted to lowercase and vice versa. |
title | title() | Returns a titlecased version of the original string, i.e. all words start with uppercase and all remaining cased characters are lowercase. |
translate | translate(table[, deletechars]) | Returns a copy of the string in which all the characters in delchars are deleted and the remaining characters are converted according to table, which must be a string of length 256. |
upper | upper() | Returns a copy of the string with all cased characters converted to uppercase. |
Table 2.5. List of list methods. See List Methods for reference.
Method name | Syntax | Description |
---|---|---|
append | s.append(x) | Append the element x to the end of list s |
extend | s.extend(x) | Append the the elements from the list x to the end of list s |
count | s.count(x) | Returns the number of occurences of the element x in the list s |
index | s.index(x) | Returns the index of the first occurence of the element x in the list s |
insert | s.insert(i, x) | Inserts the element x into the list s at index i |
pop | s.pop([i]) | Removes the element at index i from the list s |
remove | s.remove(x) | Removes the element x fro the list s |
reverse | s.reverse() | Reverses the order of the element in the list s |
sort | s.sort([cmpfunc]) | Sorts the elements of the list s using the (optional) comparison function cmpfunc |
Table 2.6. List of dictionary methods. See Dictionary Methods for reference.
Method name | Syntax | Description |
---|---|---|
clear | a.clear() | Removes all items from the dictionary a. |
copy | a.copy() | Returns a copy of the dictionary a. |
del | del a[k] | Removes the item with the key k from the dictionary a. |
get | a.get(k[, x]) | Returns a[k] if the key k is in the dictionary a and x (or None if x is omitted) otherwise. |
has_key | a.has_key(k) | Returns true (1) if a pair with the key k is in the dictionary a. |
items | a.items() | Returns a list of (key, value) pairs corresponding to all the elements of the dictionary a. |
keys | a.keys() | Returns a list of all keys in the dictionary a. |
len | len(a) | Returns the number of items (entries) in the dictionary a |
setdefault | a.setdefault(k[, x]) | The returned value is the same as for get. Additionally, if an element with the key k is not present in the dictionary a, an element k: x is added to a. |
update | a.update(b) | Changes the value of every item in the dictionary a with the same key as some item in the dictionary b to the value of the said item in b. |
values | a.values() | Returns a list of the values of all items in the dictionary a. |
popitem | a.popitem() | Returns an arbitraty (key, value) pair and removes it from the dictionary a. |