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]
Template A string class for supporting $-substitutions.
_TemplateMetaclass  
_multimap Helper class for combining multiple mappings.

Functions [hide private]
  atof(s)
Return the floating point number represented by the string s.
  atoi(s, base=...)
Return the integer represented by the string s in the given base, which defaults to 10.
  atol(s, base=...)
Return the long integer represented by the string s in the given base, which defaults to 10.
  capitalize(s)
Return a copy of the string s with only its first character capitalized.
  capwords(s, sep={})
capwords(s, [sep]) -> string
  center(s, width, fillchar=...)
Return a center version of s, in a field of the specified width.
  count(s, sub, start=..., end=...)
Return the number of occurrences of substring sub in string s[start:end].
  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).
  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].
  index(s, sub, start=... , end=...)
Like find but raises ValueError when the substring is not found.
  join(list, sep=...)
Return a string composed of the words in list, with intervening occurrences of sep.
  joinfields(list, sep=...)
Return a string composed of the words in list, with intervening occurrences of sep.
  ljust(s, width, fillchar=...)
Return a left-justified version of s, in a field of the specified width, padded with spaces as needed.
  lower(s)
Return a copy of the string s converted to lowercase.
  lstrip(s, chars=...)
Return a copy of the string s with leading whitespace removed.
  replace(s, old, new, maxsplit=-1)
replace (str, old, new[, maxsplit]) -> string
  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].
  rindex(s, sub, start=... , end=...)
Like rfind but raises ValueError when the substring is not found.
  rjust(s, width, fillchar=...)
Return a right-justified version of s, in a field of the specified width, padded with spaces as needed.
  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.
  rstrip(s, chars=...)
Return a copy of the string s with trailing whitespace removed.
  split(s, sep=... , maxsplit=...)
Return a list of the words in the string s, using sep as the delimiter string.
  splitfields(s, sep=... , maxsplit=...)
Return a list of the words in the string s, using sep as the delimiter string.
  strip(s, chars=...)
Return a copy of the string s with leading and trailing whitespace removed.
  swapcase(s)
Return a copy of the string s with upper case characters converted to lowercase and vice versa.
  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.
  upper(s)
Return a copy of the string s converted to uppercase.
  zfill(x, width)
Pad a numeric string x with zeros on the left, to fill a field of the specified width.

Variables [hide private]
_idmap  
_idmapL  
ascii_letters  
ascii_lowercase  
ascii_uppercase  
digits  
hexdigits  
letters  
lowercase  
octdigits  
printable  
punctuation  
uppercase  
whitespace  

Function Details [hide private]

atof(s)

 
Return the floating point number represented by the string s.
Returns:
float

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

capitalize(s)

 
Return a copy of the string s with only its first character capitalized.
Returns:
string

capwords(s, sep={})

 

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.

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

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

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).
Returns:
string

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

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

 
Like find but raises ValueError when the substring is not found.
Returns:
int

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

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

lower(s)

 
Return a copy of the string s converted to lowercase.
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

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.

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

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

 
Like rfind but raises ValueError when the substring is not found.
Returns:
int

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

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

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

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

swapcase(s)

 
Return a copy of the string s with upper case characters converted to lowercase and vice versa.
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

upper(s)

 
Return a copy of the string s converted to uppercase.
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


Variables Details [hide private]

_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\
...                                                                    
      

_idmapL

Value:
{}                                                                     
      

ascii_letters

Value:
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'                 
      

ascii_lowercase

Value:
'abcdefghijklmnopqrstuvwxyz'                                           
      

ascii_uppercase

Value:
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                           
      

digits

Value:
'0123456789'                                                           
      

hexdigits

Value:
'0123456789abcdefABCDEF'                                               
      

letters

Value:
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'                 
      

lowercase

Value:
'abcdefghijklmnopqrstuvwxyz'                                           
      

octdigits

Value:
'01234567'                                                             
      

printable

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

punctuation

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

uppercase

Value:
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                           
      

whitespace

Value:
'''\t
\x0b\x0c\r '''