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

Module string

source code

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]
  Formatter
  Template
A string class for supporting $-substitutions.
  _TemplateMetaclass
  _multimap
Helper class for combining multiple mappings.
Functions [hide private]
float
atof(s)
Return the floating point number represented by the string s.
source code
int
atoi(s, base=...)
Return the integer represented by the string s in the given base, which defaults to 10.
source code
long
atol(s, base=...)
Return the long integer represented by the string s in the given base, which defaults to 10.
source code
string
capitalize(s)
Return a copy of the string s with only its first character capitalized.
source code
string
capwords(s, sep=...)
Split the argument into words using split, capitalize each word using capitalize, and join the capitalized words using join.
source code
string
center(s, width, fillchar=...)
Return a center version of s, in a field of the specified width.
source code
int
count(s, sub, start=..., end=...)
Return the number of occurrences of substring sub in string s[start:end].
source code
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).
source code
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].
source code
int
index(s, sub, start=... , end=...)
Like find but raises ValueError when the substring is not found.
source code
string
join(list, sep=...)
Return a string composed of the words in list, with intervening occurrences of sep.
source code
string
joinfields(list, sep=...)
Return a string composed of the words in list, with intervening occurrences of sep.
source code
string
ljust(s, width, fillchar=...)
Return a left-justified version of s, in a field of the specified width, padded with spaces as needed.
source code
string
lower(s)
Return a copy of the string s converted to lowercase.
source code
string
lstrip(s, chars=...)
Return a copy of the string s with leading whitespace removed.
source code
 
replace(s, old, new, maxreplace=-1)
replace (str, old, new[, maxreplace]) -> string
source code
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].
source code
int
rindex(s, sub, start=... , end=...)
Like rfind but raises ValueError when the substring is not found.
source code
string
rjust(s, width, fillchar=...)
Return a right-justified version of s, in a field of the specified width, padded with spaces as needed.
source code
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.
source code
string
rstrip(s, chars=...)
Return a copy of the string s with trailing whitespace removed.
source code
list of strings
split(s, sep=... , maxsplit=...)
Return a list of the words in the string s, using sep as the delimiter string.
source code
list of strings
splitfields(s, sep=... , maxsplit=...)
Return a list of the words in the string s, using sep as the delimiter string.
source code
string
strip(s, chars=...)
Return a copy of the string s with leading and trailing whitespace removed.
source code
string
swapcase(s)
Return a copy of the string s with upper case characters converted to lowercase and vice versa.
source code
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.
source code
string
upper(s)
Return a copy of the string s converted to uppercase.
source code
string
zfill(x, width)
Pad a numeric string x with zeros on the left, to fill a field of the specified width.
source code
Variables [hide private]
  __package__ = None
hash(x)
  _idmap = '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x...
  _idmapL = None
hash(x)
  ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS...
  ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
  ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  digits = '0123456789'
  hexdigits = '0123456789abcdefABCDEF'
  letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  lowercase = 'abcdefghijklmnopqrstuvwxyz'
  octdigits = '01234567'
  printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM...
  punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
  uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  whitespace = '\t\n\x0b\x0c\r '
Function Details [hide private]

atoi(s, base=...)

source code 

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=...)

source code 

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

capwords(s, sep=...)

source code 

Split the argument into words using split, capitalize each word using capitalize, and join the capitalized words using join. If the optional second argument sep is absent or None, runs of whitespace characters are replaced by a single space and leading and trailing whitespace are removed, otherwise sep is used to split and join the words.

Returns: string

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

source code 

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=...)

source code 

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=...)

source code 

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

join(list, sep=...)

source code 

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=...)

source code 

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=...)

source code 

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

lstrip(s, chars=...)

source code 

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, maxreplace=-1)

source code 

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

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

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

source code 

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

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

source code 

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=...)

source code 

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=...)

source code 

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=...)

source code 

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=...)

source code 

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=...)

source code 

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

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

source code 

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

zfill(x, width)

source code 

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

ascii_letters

Value:
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

printable

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