Module string
[hide private]
[frames] | no frames]

Module string

A collection of string operations (most are no longer used).

Warning: most of the code you see here isn't normally used nowadays. Beginning with Python 1.6, many of these functions are implemented as methods on the standard string object. They used to be implemented by a built-in module called strop, but strop is now obsolete itself.

Public module variables:

whitespace -- a string containing all characters considered whitespace lowercase -- a string containing all characters considered lowercase letters uppercase -- a string containing all characters considered uppercase letters letters -- a string containing all characters considered letters digits -- a string containing all characters considered decimal digits hexdigits -- a string containing all characters considered hexadecimal digits octdigits -- a string containing all characters considered octal digits punctuation -- a string containing all characters considered punctuation printable -- a string containing all characters considered printable

Classes [hide private]
_multimap
Helper class for combining multiple mappings.
_TemplateMetaclass
Template
A string class for supporting $-substitutions.
Functions [hide private]
 
capwords(s, sep=None)
capwords(s, [sep]) -> string
string
lower(s)
Return a copy of the string s converted to lowercase.
string
upper(s)
Return a copy of the string s converted to uppercase.
string
swapcase(s)
Return a copy of the string s with upper case characters converted to lowercase and vice versa.
string
strip(s, chars=...)
Return a copy of the string s with leading and trailing whitespace removed.
string
lstrip(s, chars=...)
Return a copy of the string s with leading whitespace removed.
string
rstrip(s, chars=...)
Return a copy of the string s with trailing whitespace removed.
list of strings
split(s, sep=... , maxsplit=...)
Return a list of the words in the string s, using sep as the delimiter string.
list of strings
splitfields(s, sep=... , maxsplit=...)
Return a list of the words in the string s, using sep as the delimiter string.
list of strings
rsplit(s, sep=... , maxsplit=...)
Return a list of the words in the string s, using sep as the delimiter string, starting at the end of the string and working to the front.
string
join(list, sep=...)
Return a string composed of the words in list, with intervening occurrences of sep.
string
joinfields(list, sep=...)
Return a string composed of the words in list, with intervening occurrences of sep.
int
index(s, sub, start=... , end=...)
Like find but raises ValueError when the substring is not found.
int
rindex(s, sub, start=... , end=...)
Like rfind but raises ValueError when the substring is not found.
int
count(s, sub, start=..., end=...)
Return the number of occurrences of substring sub in string s[start:end].
in
find(s, sub, start=... , end=...)
Return the lowest index in s where substring sub is found, such that sub is contained within s[start,end].
int
rfind(s, sub, start=... , end=...)
Return the highest index in s where substring sub is found, such that sub is contained within s[start,end].
float
atof(s)
Return the floating point number represented by the string s.
int
atoi(s, base=...)
Return the integer represented by the string s in the given base, which defaults to 10.
long
atol(s, base=...)
Return the long integer represented by the string s in the given base, which defaults to 10.
string
ljust(s, width, fillchar=...)
Return a left-justified version of s, in a field of the specified width, padded with spaces as needed.
string
rjust(s, width, fillchar=...)
Return a right-justified version of s, in a field of the specified width, padded with spaces as needed.
string
center(s, width, fillchar=...)
Return a center version of s, in a field of the specified width.
string
zfill(x, width)
Pad a numeric string x with zeros on the left, to fill a field of the specified width.
string
expandtabs(s, tabsize=...)
Return a copy of the string s with all tab characters replaced by the appropriate number of spaces, depending on the current column, and the tabsize (default 8).
string
translate(s, table, deletions=...)
Return a copy of the string s, where all characters occurring in the optional argument deletions are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256.
string
capitalize(s)
Return a copy of the string s with only its first character capitalized.
 
replace(s, old, new, maxsplit=-1)
replace (str, old, new[, maxsplit]) -> string
Variables [hide private]
  ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
  ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS...
  digits = '0123456789'
  hexdigits = '0123456789abcdefABCDEF'
  octdigits = '01234567'
  punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM...
  _idmap = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x...
  _idmapL = None
  letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

Imports: _re, index_error, atoi_error, atof_error, atol_error, _float, _int, _long, maketrans, lowercase, uppercase, whitespace


Function Details [hide private]

capwords(s, sep=None)

 

capwords(s, [sep]) -> string

Split the argument into words using split, capitalize each word using capitalize, and join the capitalized words using join. Note that this replaces runs of whitespace characters by a single space.

strip(s, chars=...)

 

Return a copy of the string s with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping.

Returns: string

lstrip(s, chars=...)

 

Return a copy of the string s with leading whitespace removed. If chars is given and not None, remove characters in chars instead.

Returns: string

rstrip(s, chars=...)

 

Return a copy of the string s with trailing whitespace removed. If chars is given and not None, remove characters in chars instead.

Returns: string

split(s, sep=... , maxsplit=...)

 

Return a list of the words in the string s, using sep as the delimiter string. If maxsplit is given, splits at no more than maxsplit places (resulting in at most maxsplit+1 words). If sep is not specified or is None, any whitespace string is a separator.

(split and splitfields are synonymous)

Returns: list of strings

splitfields(s, sep=... , maxsplit=...)

 

Return a list of the words in the string s, using sep as the delimiter string. If maxsplit is given, splits at no more than maxsplit places (resulting in at most maxsplit+1 words). If sep is not specified or is None, any whitespace string is a separator.

(split and splitfields are synonymous)

Returns: list of strings

rsplit(s, sep=... , maxsplit=...)

 

Return a list of the words in the string s, using sep as the delimiter string, starting at the end of the string and working to the front. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator.

Returns: list of strings

join(list, sep=...)

 

Return a string composed of the words in list, with intervening occurrences of sep. The default separator is a single space.

(joinfields and join are synonymous)

Returns: string

joinfields(list, sep=...)

 

Return a string composed of the words in list, with intervening occurrences of sep. The default separator is a single space.

(joinfields and join are synonymous)

Returns: string

count(s, sub, start=..., end=...)

 

Return the number of occurrences of substring sub in string s[start:end]. Optional arguments start and end are interpreted as in slice notation.

Returns: int

find(s, sub, start=... , end=...)

 

Return the lowest index in s where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation.

Return -1 on failure.

Returns: in

rfind(s, sub, start=... , end=...)

 

Return the highest index in s where substring sub is found, such that sub is contained within s[start,end]. Optional arguments start and end are interpreted as in slice notation.

Return -1 on failure.

Returns: int

atoi(s, base=...)

 

Return the integer represented by the string s in the given base, which defaults to 10. The string s must consist of one or more digits, possibly preceded by a sign. If base is 0, it is chosen from the leading characters of s, 0 for octal, 0x or 0X for hexadecimal. If base is 16, a preceding 0x or 0X is accepted.

Returns: int

atol(s, base=...)

 

Return the long integer represented by the string s in the given base, which defaults to 10. The string s must consist of one or more digits, possibly preceded by a sign. If base is 0, it is chosen from the leading characters of s, 0 for octal, 0x or 0X for hexadecimal. If base is 16, a preceding 0x or 0X is accepted. A trailing L or l is not accepted, unless base is 0.

Returns: long

ljust(s, width, fillchar=...)

 

Return a left-justified version of s, in a field of the specified width, padded with spaces as needed. The string is never truncated. If specified the fillchar is used instead of spaces.

Returns: string

rjust(s, width, fillchar=...)

 

Return a right-justified version of s, in a field of the specified width, padded with spaces as needed. The string is never truncated. If specified the fillchar is used instead of spaces.

Returns: string

center(s, width, fillchar=...)

 

Return a center version of s, in a field of the specified width. padded with spaces as needed. The string is never truncated. If specified the fillchar is used instead of spaces.

Returns: string

zfill(x, width)

 

Pad a numeric string x with zeros on the left, to fill a field of the specified width. The string x is never truncated.

Returns: string

translate(s, table, deletions=...)

 

Return a copy of the string s, where all characters occurring in the optional argument deletions are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256. The deletions argument is not allowed for Unicode strings.

Returns: string

replace(s, old, new, maxsplit=-1)

 

replace (str, old, new[, maxsplit]) -> string

Return a copy of string str with all occurrences of substring old replaced by new. If the optional argument maxsplit is given, only the first maxsplit occurrences are replaced.


Variables Details [hide private]

ascii_letters

Value:
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

printable

Value:
'''0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%\
&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t
\r\x0b\x0c'''

_idmap

Value:
'''\x00\x01\x02\x03\x04\x05\x06\x07\x08\t
\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\
\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX\
YZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x8\
6\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\\
x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa\
9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\\
xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xc\
...