Sophie

Sophie

distrib > Mandriva > current > i586 > media > main-updates > by-pkgid > 8f1462e52e1797a02c97073eed0b7f92 > files > 895

python-docs-2.6.5-2.5mdv2010.2.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8.1. string — Common string operations &mdash; Python v2.6.5 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.5',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.5 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.5 documentation" href="../index.html" />
    <link rel="up" title="8. String Services" href="strings.html" />
    <link rel="next" title="8.2. re — Regular expression operations" href="re.html" />
    <link rel="prev" title="8. String Services" href="strings.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="re.html" title="8.2. re — Regular expression operations"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="strings.html" title="8. String Services"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" accesskey="U">8. String Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-string">
<h1>8.1. <tt class="xref docutils literal"><span class="pre">string</span></tt> &#8212; Common string operations<a class="headerlink" href="#module-string" title="Permalink to this headline">¶</a></h1>
<p id="index-619">The <tt class="xref docutils literal"><span class="pre">string</span></tt> module contains a number of useful constants and
classes, as well as some deprecated legacy functions that are also
available as methods on strings. In addition, Python&#8217;s built-in string
classes support the sequence type methods described in the
<a class="reference external" href="stdtypes.html#typesseq"><em>Sequence Types &#8212; str, unicode, list, tuple, buffer, xrange</em></a> section, and also the string-specific methods described
in the <a class="reference external" href="stdtypes.html#string-methods"><em>String Methods</em></a> section. To output formatted strings use
template strings or the <tt class="docutils literal"><span class="pre">%</span></tt> operator described in the
<a class="reference external" href="stdtypes.html#string-formatting"><em>String Formatting Operations</em></a> section. Also, see the <a title="Regular expression operations." class="reference external" href="re.html#module-re"><tt class="xref docutils literal"><span class="pre">re</span></tt></a> module for
string functions based on regular expressions.</p>
<div class="section" id="string-constants">
<h2>8.1.1. String constants<a class="headerlink" href="#string-constants" title="Permalink to this headline">¶</a></h2>
<p>The constants defined in this module are:</p>
<dl class="data">
<dt id="string.ascii_letters">
<tt class="descclassname">string.</tt><tt class="descname">ascii_letters</tt><a class="headerlink" href="#string.ascii_letters" title="Permalink to this definition">¶</a></dt>
<dd>The concatenation of the <a title="string.ascii_lowercase" class="reference internal" href="#string.ascii_lowercase"><tt class="xref docutils literal"><span class="pre">ascii_lowercase</span></tt></a> and <a title="string.ascii_uppercase" class="reference internal" href="#string.ascii_uppercase"><tt class="xref docutils literal"><span class="pre">ascii_uppercase</span></tt></a>
constants described below.  This value is not locale-dependent.</dd></dl>

<dl class="data">
<dt id="string.ascii_lowercase">
<tt class="descclassname">string.</tt><tt class="descname">ascii_lowercase</tt><a class="headerlink" href="#string.ascii_lowercase" title="Permalink to this definition">¶</a></dt>
<dd>The lowercase letters <tt class="docutils literal"><span class="pre">'abcdefghijklmnopqrstuvwxyz'</span></tt>.  This value is not
locale-dependent and will not change.</dd></dl>

<dl class="data">
<dt id="string.ascii_uppercase">
<tt class="descclassname">string.</tt><tt class="descname">ascii_uppercase</tt><a class="headerlink" href="#string.ascii_uppercase" title="Permalink to this definition">¶</a></dt>
<dd>The uppercase letters <tt class="docutils literal"><span class="pre">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span></tt>.  This value is not
locale-dependent and will not change.</dd></dl>

<dl class="data">
<dt id="string.digits">
<tt class="descclassname">string.</tt><tt class="descname">digits</tt><a class="headerlink" href="#string.digits" title="Permalink to this definition">¶</a></dt>
<dd>The string <tt class="docutils literal"><span class="pre">'0123456789'</span></tt>.</dd></dl>

<dl class="data">
<dt id="string.hexdigits">
<tt class="descclassname">string.</tt><tt class="descname">hexdigits</tt><a class="headerlink" href="#string.hexdigits" title="Permalink to this definition">¶</a></dt>
<dd>The string <tt class="docutils literal"><span class="pre">'0123456789abcdefABCDEF'</span></tt>.</dd></dl>

<dl class="data">
<dt id="string.letters">
<tt class="descclassname">string.</tt><tt class="descname">letters</tt><a class="headerlink" href="#string.letters" title="Permalink to this definition">¶</a></dt>
<dd>The concatenation of the strings <a title="string.lowercase" class="reference internal" href="#string.lowercase"><tt class="xref docutils literal"><span class="pre">lowercase</span></tt></a> and <a title="string.uppercase" class="reference internal" href="#string.uppercase"><tt class="xref docutils literal"><span class="pre">uppercase</span></tt></a>
described below.  The specific value is locale-dependent, and will be updated
when <a title="locale.setlocale" class="reference external" href="locale.html#locale.setlocale"><tt class="xref docutils literal"><span class="pre">locale.setlocale()</span></tt></a> is called.</dd></dl>

<dl class="data">
<dt id="string.lowercase">
<tt class="descclassname">string.</tt><tt class="descname">lowercase</tt><a class="headerlink" href="#string.lowercase" title="Permalink to this definition">¶</a></dt>
<dd>A string containing all the characters that are considered lowercase letters.
On most systems this is the string <tt class="docutils literal"><span class="pre">'abcdefghijklmnopqrstuvwxyz'</span></tt>.  The
specific value is locale-dependent, and will be updated when
<a title="locale.setlocale" class="reference external" href="locale.html#locale.setlocale"><tt class="xref docutils literal"><span class="pre">locale.setlocale()</span></tt></a> is called.</dd></dl>

<dl class="data">
<dt id="string.octdigits">
<tt class="descclassname">string.</tt><tt class="descname">octdigits</tt><a class="headerlink" href="#string.octdigits" title="Permalink to this definition">¶</a></dt>
<dd>The string <tt class="docutils literal"><span class="pre">'01234567'</span></tt>.</dd></dl>

<dl class="data">
<dt id="string.punctuation">
<tt class="descclassname">string.</tt><tt class="descname">punctuation</tt><a class="headerlink" href="#string.punctuation" title="Permalink to this definition">¶</a></dt>
<dd>String of ASCII characters which are considered punctuation characters in the
<tt class="docutils literal"><span class="pre">C</span></tt> locale.</dd></dl>

<dl class="data">
<dt id="string.printable">
<tt class="descclassname">string.</tt><tt class="descname">printable</tt><a class="headerlink" href="#string.printable" title="Permalink to this definition">¶</a></dt>
<dd>String of characters which are considered printable.  This is a combination of
<a title="string.digits" class="reference internal" href="#string.digits"><tt class="xref docutils literal"><span class="pre">digits</span></tt></a>, <a title="string.letters" class="reference internal" href="#string.letters"><tt class="xref docutils literal"><span class="pre">letters</span></tt></a>, <a title="string.punctuation" class="reference internal" href="#string.punctuation"><tt class="xref docutils literal"><span class="pre">punctuation</span></tt></a>, and
<a title="string.whitespace" class="reference internal" href="#string.whitespace"><tt class="xref docutils literal"><span class="pre">whitespace</span></tt></a>.</dd></dl>

<dl class="data">
<dt id="string.uppercase">
<tt class="descclassname">string.</tt><tt class="descname">uppercase</tt><a class="headerlink" href="#string.uppercase" title="Permalink to this definition">¶</a></dt>
<dd>A string containing all the characters that are considered uppercase letters.
On most systems this is the string <tt class="docutils literal"><span class="pre">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span></tt>.  The
specific value is locale-dependent, and will be updated when
<a title="locale.setlocale" class="reference external" href="locale.html#locale.setlocale"><tt class="xref docutils literal"><span class="pre">locale.setlocale()</span></tt></a> is called.</dd></dl>

<dl class="data">
<dt id="string.whitespace">
<tt class="descclassname">string.</tt><tt class="descname">whitespace</tt><a class="headerlink" href="#string.whitespace" title="Permalink to this definition">¶</a></dt>
<dd>A string containing all characters that are considered whitespace. On most
systems this includes the characters space, tab, linefeed, return, formfeed, and
vertical tab.</dd></dl>

</div>
<div class="section" id="string-formatting">
<span id="new-string-formatting"></span><h2>8.1.2. String Formatting<a class="headerlink" href="#string-formatting" title="Permalink to this headline">¶</a></h2>
<p>Starting in Python 2.6, the built-in str and unicode classes provide the ability
to do complex variable substitutions and value formatting via the
<a title="str.format" class="reference external" href="stdtypes.html#str.format"><tt class="xref docutils literal"><span class="pre">str.format()</span></tt></a> method described in <span class="target" id="index-620"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>.  The <a title="string.Formatter" class="reference internal" href="#string.Formatter"><tt class="xref docutils literal"><span class="pre">Formatter</span></tt></a>
class in the <tt class="xref docutils literal"><span class="pre">string</span></tt> module allows you to create and customize your own
string formatting behaviors using the same implementation as the built-in
<a title="format" class="reference external" href="functions.html#format"><tt class="xref docutils literal"><span class="pre">format()</span></tt></a> method.</p>
<dl class="class">
<dt id="string.Formatter">
<em class="property">class </em><tt class="descclassname">string.</tt><tt class="descname">Formatter</tt><a class="headerlink" href="#string.Formatter" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a title="string.Formatter" class="reference internal" href="#string.Formatter"><tt class="xref docutils literal"><span class="pre">Formatter</span></tt></a> class has the following public methods:</p>
<dl class="method">
<dt id="string.Formatter.format">
<tt class="descname">format</tt><big>(</big><em>format_string</em>, <em>*args</em>, <em>*kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.format" title="Permalink to this definition">¶</a></dt>
<dd><a title="format" class="reference external" href="functions.html#format"><tt class="xref docutils literal"><span class="pre">format()</span></tt></a> is the primary API method.  It takes a format template
string, and an arbitrary set of positional and keyword argument.
<a title="format" class="reference external" href="functions.html#format"><tt class="xref docutils literal"><span class="pre">format()</span></tt></a> is just a wrapper that calls <a title="string.Formatter.vformat" class="reference internal" href="#string.Formatter.vformat"><tt class="xref docutils literal"><span class="pre">vformat()</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="string.Formatter.vformat">
<tt class="descname">vformat</tt><big>(</big><em>format_string</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.vformat" title="Permalink to this definition">¶</a></dt>
<dd>This function does the actual work of formatting.  It is exposed as a
separate function for cases where you want to pass in a predefined
dictionary of arguments, rather than unpacking and repacking the
dictionary as individual arguments using the <tt class="docutils literal"><span class="pre">*args</span></tt> and <tt class="docutils literal"><span class="pre">**kwds</span></tt>
syntax.  <a title="string.Formatter.vformat" class="reference internal" href="#string.Formatter.vformat"><tt class="xref docutils literal"><span class="pre">vformat()</span></tt></a> does the work of breaking up the format template
string into character data and replacement fields.  It calls the various
methods described below.</dd></dl>

<p>In addition, the <a title="string.Formatter" class="reference internal" href="#string.Formatter"><tt class="xref docutils literal"><span class="pre">Formatter</span></tt></a> defines a number of methods that are
intended to be replaced by subclasses:</p>
<dl class="method">
<dt id="string.Formatter.parse">
<tt class="descname">parse</tt><big>(</big><em>format_string</em><big>)</big><a class="headerlink" href="#string.Formatter.parse" title="Permalink to this definition">¶</a></dt>
<dd><p>Loop over the format_string and return an iterable of tuples
(<em>literal_text</em>, <em>field_name</em>, <em>format_spec</em>, <em>conversion</em>).  This is used
by <a title="string.Formatter.vformat" class="reference internal" href="#string.Formatter.vformat"><tt class="xref docutils literal"><span class="pre">vformat()</span></tt></a> to break the string in to either literal text, or
replacement fields.</p>
<p>The values in the tuple conceptually represent a span of literal text
followed by a single replacement field.  If there is no literal text
(which can happen if two replacement fields occur consecutively), then
<em>literal_text</em> will be a zero-length string.  If there is no replacement
field, then the values of <em>field_name</em>, <em>format_spec</em> and <em>conversion</em>
will be <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="string.Formatter.get_field">
<tt class="descname">get_field</tt><big>(</big><em>field_name</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.get_field" title="Permalink to this definition">¶</a></dt>
<dd>Given <em>field_name</em> as returned by <a title="string.Formatter.parse" class="reference internal" href="#string.Formatter.parse"><tt class="xref docutils literal"><span class="pre">parse()</span></tt></a> (see above), convert it to
an object to be formatted.  Returns a tuple (obj, used_key).  The default
version takes strings of the form defined in <span class="target" id="index-621"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a>, such as
&#8220;0[name]&#8221; or &#8220;label.title&#8221;.  <em>args</em> and <em>kwargs</em> are as passed in to
<a title="string.Formatter.vformat" class="reference internal" href="#string.Formatter.vformat"><tt class="xref docutils literal"><span class="pre">vformat()</span></tt></a>.  The return value <em>used_key</em> has the same meaning as the
<em>key</em> parameter to <a title="string.Formatter.get_value" class="reference internal" href="#string.Formatter.get_value"><tt class="xref docutils literal"><span class="pre">get_value()</span></tt></a>.</dd></dl>

<dl class="method">
<dt id="string.Formatter.get_value">
<tt class="descname">get_value</tt><big>(</big><em>key</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.get_value" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve a given field value.  The <em>key</em> argument will be either an
integer or a string.  If it is an integer, it represents the index of the
positional argument in <em>args</em>; if it is a string, then it represents a
named argument in <em>kwargs</em>.</p>
<p>The <em>args</em> parameter is set to the list of positional arguments to
<a title="string.Formatter.vformat" class="reference internal" href="#string.Formatter.vformat"><tt class="xref docutils literal"><span class="pre">vformat()</span></tt></a>, and the <em>kwargs</em> parameter is set to the dictionary of
keyword arguments.</p>
<p>For compound field names, these functions are only called for the first
component of the field name; Subsequent components are handled through
normal attribute and indexing operations.</p>
<p>So for example, the field expression &#8216;0.name&#8217; would cause
<a title="string.Formatter.get_value" class="reference internal" href="#string.Formatter.get_value"><tt class="xref docutils literal"><span class="pre">get_value()</span></tt></a> to be called with a <em>key</em> argument of 0.  The <tt class="docutils literal"><span class="pre">name</span></tt>
attribute will be looked up after <a title="string.Formatter.get_value" class="reference internal" href="#string.Formatter.get_value"><tt class="xref docutils literal"><span class="pre">get_value()</span></tt></a> returns by calling the
built-in <a title="getattr" class="reference external" href="functions.html#getattr"><tt class="xref docutils literal"><span class="pre">getattr()</span></tt></a> function.</p>
<p>If the index or keyword refers to an item that does not exist, then an
<a title="exceptions.IndexError" class="reference external" href="exceptions.html#exceptions.IndexError"><tt class="xref docutils literal"><span class="pre">IndexError</span></tt></a> or <a title="exceptions.KeyError" class="reference external" href="exceptions.html#exceptions.KeyError"><tt class="xref docutils literal"><span class="pre">KeyError</span></tt></a> should be raised.</p>
</dd></dl>

<dl class="method">
<dt id="string.Formatter.check_unused_args">
<tt class="descname">check_unused_args</tt><big>(</big><em>used_args</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#string.Formatter.check_unused_args" title="Permalink to this definition">¶</a></dt>
<dd>Implement checking for unused arguments if desired.  The arguments to this
function is the set of all argument keys that were actually referred to in
the format string (integers for positional arguments, and strings for
named arguments), and a reference to the <em>args</em> and <em>kwargs</em> that was
passed to vformat.  The set of unused args can be calculated from these
parameters.  <a title="string.Formatter.check_unused_args" class="reference internal" href="#string.Formatter.check_unused_args"><tt class="xref docutils literal"><span class="pre">check_unused_args()</span></tt></a> is assumed to throw an exception if
the check fails.</dd></dl>

<dl class="method">
<dt id="string.Formatter.format_field">
<tt class="descname">format_field</tt><big>(</big><em>value</em>, <em>format_spec</em><big>)</big><a class="headerlink" href="#string.Formatter.format_field" title="Permalink to this definition">¶</a></dt>
<dd><a title="string.Formatter.format_field" class="reference internal" href="#string.Formatter.format_field"><tt class="xref docutils literal"><span class="pre">format_field()</span></tt></a> simply calls the global <a title="format" class="reference external" href="functions.html#format"><tt class="xref docutils literal"><span class="pre">format()</span></tt></a> built-in.  The
method is provided so that subclasses can override it.</dd></dl>

<dl class="method">
<dt id="string.Formatter.convert_field">
<tt class="descname">convert_field</tt><big>(</big><em>value</em>, <em>conversion</em><big>)</big><a class="headerlink" href="#string.Formatter.convert_field" title="Permalink to this definition">¶</a></dt>
<dd>Converts the value (returned by <a title="string.Formatter.get_field" class="reference internal" href="#string.Formatter.get_field"><tt class="xref docutils literal"><span class="pre">get_field()</span></tt></a>) given a conversion type
(as in the tuple returned by the <a title="string.Formatter.parse" class="reference internal" href="#string.Formatter.parse"><tt class="xref docutils literal"><span class="pre">parse()</span></tt></a> method.)  The default
version understands &#8216;r&#8217; (repr) and &#8216;s&#8217; (str) conversion types.</dd></dl>

</dd></dl>

</div>
<div class="section" id="format-string-syntax">
<span id="formatstrings"></span><h2>8.1.3. Format String Syntax<a class="headerlink" href="#format-string-syntax" title="Permalink to this headline">¶</a></h2>
<p>The <a title="str.format" class="reference external" href="stdtypes.html#str.format"><tt class="xref docutils literal"><span class="pre">str.format()</span></tt></a> method and the <a title="string.Formatter" class="reference internal" href="#string.Formatter"><tt class="xref docutils literal"><span class="pre">Formatter</span></tt></a> class share the same
syntax for format strings (although in the case of <a title="string.Formatter" class="reference internal" href="#string.Formatter"><tt class="xref docutils literal"><span class="pre">Formatter</span></tt></a>,
subclasses can define their own format string syntax.)</p>
<p>Format strings contain &#8220;replacement fields&#8221; surrounded by curly braces <tt class="docutils literal"><span class="pre">{}</span></tt>.
Anything that is not contained in braces is considered literal text, which is
copied unchanged to the output.  If you need to include a brace character in the
literal text, it can be escaped by doubling: <tt class="docutils literal"><span class="pre">{{</span></tt> and <tt class="docutils literal"><span class="pre">}}</span></tt>.</p>
<p>The grammar for a replacement field is as follows:</p>
<blockquote>
<pre>
<strong id="grammar-token-replacement_field">replacement_field</strong> ::=  &quot;{&quot; <a class="reference internal" href="#grammar-token-field_name"><tt class="xref docutils literal"><span class="pre">field_name</span></tt></a> [&quot;!&quot; <a class="reference internal" href="#grammar-token-conversion"><tt class="xref docutils literal"><span class="pre">conversion</span></tt></a>] [&quot;:&quot; <a class="reference internal" href="#grammar-token-format_spec"><tt class="xref docutils literal"><span class="pre">format_spec</span></tt></a>] &quot;}&quot;
<strong id="grammar-token-field_name">field_name       </strong> ::=  (<a class="reference external" href="../reference/lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a> | <a class="reference external" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a>) (&quot;.&quot; <a class="reference internal" href="#grammar-token-attribute_name"><tt class="xref docutils literal"><span class="pre">attribute_name</span></tt></a> | &quot;[&quot; <a class="reference internal" href="#grammar-token-element_index"><tt class="xref docutils literal"><span class="pre">element_index</span></tt></a> &quot;]&quot;)*
<strong id="grammar-token-attribute_name">attribute_name   </strong> ::=  <a class="reference external" href="../reference/lexical_analysis.html#grammar-token-identifier"><tt class="xref docutils literal"><span class="pre">identifier</span></tt></a>
<strong id="grammar-token-element_index">element_index    </strong> ::=  <a class="reference external" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a> | <a class="reference internal" href="#grammar-token-index_string"><tt class="xref docutils literal"><span class="pre">index_string</span></tt></a>
<strong id="grammar-token-index_string">index_string     </strong> ::=  &lt;any source character except &quot;]&quot;&gt; +
<strong id="grammar-token-conversion">conversion       </strong> ::=  &quot;r&quot; | &quot;s&quot;
<strong id="grammar-token-format_spec">format_spec      </strong> ::=  &lt;described in the next section&gt;
</pre>
</blockquote>
<p>In less formal terms, the replacement field starts with a <em>field_name</em>, which
can either be a number (for a positional argument), or an identifier (for
keyword arguments).  Following this is an optional <em>conversion</em> field, which is
preceded by an exclamation point <tt class="docutils literal"><span class="pre">'!'</span></tt>, and a <em>format_spec</em>, which is preceded
by a colon <tt class="docutils literal"><span class="pre">':'</span></tt>.</p>
<p>The <em>field_name</em> itself begins with either a number or a keyword.  If it&#8217;s a
number, it refers to a positional argument, and if it&#8217;s a keyword it refers to a
named keyword argument.  This can be followed by any number of index or
attribute expressions. An expression of the form <tt class="docutils literal"><span class="pre">'.name'</span></tt> selects the named
attribute using <a title="getattr" class="reference external" href="functions.html#getattr"><tt class="xref docutils literal"><span class="pre">getattr()</span></tt></a>, while an expression of the form <tt class="docutils literal"><span class="pre">'[index]'</span></tt>
does an index lookup using <a title="object.__getitem__" class="reference external" href="../reference/datamodel.html#object.__getitem__"><tt class="xref docutils literal"><span class="pre">__getitem__()</span></tt></a>.</p>
<p>Some simple format string examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;First, thou shalt count to {0}&quot;</span> <span class="c"># References first positional argument</span>
<span class="s">&quot;My quest is {name}&quot;</span>             <span class="c"># References keyword argument &#39;name&#39;</span>
<span class="s">&quot;Weight in tons {0.weight}&quot;</span>      <span class="c"># &#39;weight&#39; attribute of first positional arg</span>
<span class="s">&quot;Units destroyed: {players[0]}&quot;</span>  <span class="c"># First element of keyword argument &#39;players&#39;.</span>
</pre></div>
</div>
<p>The <em>conversion</em> field causes a type coercion before formatting.  Normally, the
job of formatting a value is done by the <tt class="xref docutils literal"><span class="pre">__format__()</span></tt> method of the value
itself.  However, in some cases it is desirable to force a type to be formatted
as a string, overriding its own definition of formatting.  By converting the
value to a string before calling <tt class="xref docutils literal"><span class="pre">__format__()</span></tt>, the normal formatting logic
is bypassed.</p>
<p>Two conversion flags are currently supported: <tt class="docutils literal"><span class="pre">'!s'</span></tt> which calls <a title="str" class="reference external" href="functions.html#str"><tt class="xref docutils literal"><span class="pre">str()</span></tt></a>
on the value, and <tt class="docutils literal"><span class="pre">'!r'</span></tt> which calls <a title="repr" class="reference external" href="functions.html#repr"><tt class="xref docutils literal"><span class="pre">repr()</span></tt></a>.</p>
<p>Some examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;Harold&#39;s a clever {0!s}&quot;</span>        <span class="c"># Calls str() on the argument first</span>
<span class="s">&quot;Bring out the holy {name!r}&quot;</span>    <span class="c"># Calls repr() on the argument first</span>
</pre></div>
</div>
<p>The <em>format_spec</em> field contains a specification of how the value should be
presented, including such details as field width, alignment, padding, decimal
precision and so on.  Each value type can define its own &#8220;formatting
mini-language&#8221; or interpretation of the <em>format_spec</em>.</p>
<p>Most built-in types support a common formatting mini-language, which is
described in the next section.</p>
<p>A <em>format_spec</em> field can also include nested replacement fields within it.
These nested replacement fields can contain only a field name; conversion flags
and format specifications are not allowed.  The replacement fields within the
format_spec are substituted before the <em>format_spec</em> string is interpreted.
This allows the formatting of a value to be dynamically specified.</p>
<p>For example, suppose you wanted to have a replacement field whose field width is
determined by another variable:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;A man with two {0:{1}}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;noses&quot;</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<p>This would first evaluate the inner replacement field, making the format string
effectively:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;A man with two {0:10}&quot;</span>
</pre></div>
</div>
<p>Then the outer replacement field would be evaluated, producing:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;noses     &quot;</span>
</pre></div>
</div>
<p>Which is substituted into the string, yielding:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;A man with two noses     &quot;</span>
</pre></div>
</div>
<p>(The extra space is because we specified a field width of 10, and because left
alignment is the default for strings.)</p>
<div class="section" id="format-specification-mini-language">
<span id="formatspec"></span><h3>8.1.3.1. Format Specification Mini-Language<a class="headerlink" href="#format-specification-mini-language" title="Permalink to this headline">¶</a></h3>
<p>&#8220;Format specifications&#8221; are used within replacement fields contained within a
format string to define how individual values are presented (see
<a class="reference internal" href="#formatstrings"><em>Format String Syntax</em></a>.)  They can also be passed directly to the built-in
<a title="format" class="reference external" href="functions.html#format"><tt class="xref docutils literal"><span class="pre">format()</span></tt></a> function.  Each formattable type may define how the format
specification is to be interpreted.</p>
<p>Most built-in types implement the following options for format specifications,
although some of the formatting options are only supported by the numeric types.</p>
<p>A general convention is that an empty format string (<tt class="docutils literal"><span class="pre">&quot;&quot;</span></tt>) produces
the same result as if you had called <a title="str" class="reference external" href="functions.html#str"><tt class="xref docutils literal"><span class="pre">str()</span></tt></a> on the value. A
non-empty format string typically modifies the result.</p>
<p>The general form of a <em>standard format specifier</em> is:</p>
<pre>
<strong id="id1">format_spec</strong> ::=  [[<a class="reference internal" href="#grammar-token-fill"><tt class="xref docutils literal"><span class="pre">fill</span></tt></a>]<a class="reference internal" href="#grammar-token-align"><tt class="xref docutils literal"><span class="pre">align</span></tt></a>][<a class="reference internal" href="#grammar-token-sign"><tt class="xref docutils literal"><span class="pre">sign</span></tt></a>][#][0][<a class="reference internal" href="#grammar-token-width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a>][.<a class="reference internal" href="#grammar-token-precision"><tt class="xref docutils literal"><span class="pre">precision</span></tt></a>][<a class="reference internal" href="#grammar-token-type"><tt class="xref docutils literal"><span class="pre">type</span></tt></a>]
<strong id="grammar-token-fill">fill       </strong> ::=  &lt;a character other than '}'&gt;
<strong id="grammar-token-align">align      </strong> ::=  &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;=&quot; | &quot;^&quot;
<strong id="grammar-token-sign">sign       </strong> ::=  &quot;+&quot; | &quot;-&quot; | &quot; &quot;
<strong id="grammar-token-width">width      </strong> ::=  <a class="reference external" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a>
<strong id="grammar-token-precision">precision  </strong> ::=  <a class="reference external" href="../reference/lexical_analysis.html#grammar-token-integer"><tt class="xref docutils literal"><span class="pre">integer</span></tt></a>
<strong id="grammar-token-type">type       </strong> ::=  &quot;b&quot; | &quot;c&quot; | &quot;d&quot; | &quot;e&quot; | &quot;E&quot; | &quot;f&quot; | &quot;F&quot; | &quot;g&quot; | &quot;G&quot; | &quot;n&quot; | &quot;o&quot; | &quot;s&quot; | &quot;x&quot; | &quot;X&quot; | &quot;%&quot;
</pre>
<p>The <em>fill</em> character can be any character other than &#8216;}&#8217; (which signifies the
end of the field).  The presence of a fill character is signaled by the <em>next</em>
character, which must be one of the alignment options. If the second character
of <em>format_spec</em> is not a valid alignment option, then it is assumed that both
the fill character and the alignment option are absent.</p>
<p>The meaning of the various alignment options is as follows:</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="87%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Option</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'&lt;'</span></tt></td>
<td>Forces the field to be left-aligned within the available
space (This is the default.)</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'&gt;'</span></tt></td>
<td>Forces the field to be right-aligned within the
available space.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'='</span></tt></td>
<td>Forces the padding to be placed after the sign (if any)
but before the digits.  This is used for printing fields
in the form &#8216;+000000120&#8217;. This alignment option is only
valid for numeric types.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'^'</span></tt></td>
<td>Forces the field to be centered within the available
space.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>Note that unless a minimum field width is defined, the field width will always
be the same size as the data to fill it, so that the alignment option has no
meaning in this case.</p>
<p>The <em>sign</em> option is only valid for number types, and can be one of the
following:</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="87%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Option</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'+'</span></tt></td>
<td>indicates that a sign should be used for both
positive as well as negative numbers.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'-'</span></tt></td>
<td>indicates that a sign should be used only for negative
numbers (this is the default behavior).</td>
</tr>
<tr><td>space</td>
<td>indicates that a leading space should be used on
positive numbers, and a minus sign on negative numbers.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>The <tt class="docutils literal"><span class="pre">'#'</span></tt> option is only valid for integers, and only for binary, octal, or
hexadecimal output.  If present, it specifies that the output will be prefixed
by <tt class="docutils literal"><span class="pre">'0b'</span></tt>, <tt class="docutils literal"><span class="pre">'0o'</span></tt>, or <tt class="docutils literal"><span class="pre">'0x'</span></tt>, respectively.</p>
<p><em>width</em> is a decimal integer defining the minimum field width.  If not
specified, then the field width will be determined by the content.</p>
<p>If the <em>width</em> field is preceded by a zero (<tt class="docutils literal"><span class="pre">'0'</span></tt>) character, this enables
zero-padding.  This is equivalent to an <em>alignment</em> type of <tt class="docutils literal"><span class="pre">'='</span></tt> and a <em>fill</em>
character of <tt class="docutils literal"><span class="pre">'0'</span></tt>.</p>
<p>The <em>precision</em> is a decimal number indicating how many digits should be
displayed after the decimal point for a floating point value formatted with
<tt class="docutils literal"><span class="pre">'f'</span></tt> and <tt class="docutils literal"><span class="pre">'F'</span></tt>, or before and after the decimal point for a floating point
value formatted with <tt class="docutils literal"><span class="pre">'g'</span></tt> or <tt class="docutils literal"><span class="pre">'G'</span></tt>.  For non-number types the field
indicates the maximum field size - in other words, how many characters will be
used from the field content. The <em>precision</em> is not allowed for integer values.</p>
<p>Finally, the <em>type</em> determines how the data should be presented.</p>
<p>The available string presentation types are:</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="87%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Type</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'s'</span></tt></td>
<td>String format. This is the default type for strings and
may be omitted.</td>
</tr>
<tr><td>None</td>
<td>The same as <tt class="docutils literal"><span class="pre">'s'</span></tt>.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>The available integer presentation types are:</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="87%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Type</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'b'</span></tt></td>
<td>Binary format. Outputs the number in base 2.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'c'</span></tt></td>
<td>Character. Converts the integer to the corresponding
unicode character before printing.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'d'</span></tt></td>
<td>Decimal Integer. Outputs the number in base 10.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'o'</span></tt></td>
<td>Octal format. Outputs the number in base 8.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'x'</span></tt></td>
<td>Hex format. Outputs the number in base 16, using lower-
case letters for the digits above 9.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'X'</span></tt></td>
<td>Hex format. Outputs the number in base 16, using upper-
case letters for the digits above 9.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'n'</span></tt></td>
<td>Number. This is the same as <tt class="docutils literal"><span class="pre">'d'</span></tt>, except that it uses
the current locale setting to insert the appropriate
number separator characters.</td>
</tr>
<tr><td>None</td>
<td>The same as <tt class="docutils literal"><span class="pre">'d'</span></tt>.</td>
</tr>
</tbody>
</table>
</blockquote>
<p>In addition to the above presentation types, integers can be formatted
with the floating point presentation types listed below (except
<tt class="docutils literal"><span class="pre">'n'</span></tt> and None). When doing so, <a title="float" class="reference external" href="functions.html#float"><tt class="xref docutils literal"><span class="pre">float()</span></tt></a> is used to convert the
integer to a floating point number before formatting.</p>
<p>The available presentation types for floating point and decimal values are:</p>
<blockquote>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="87%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Type</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">'e'</span></tt></td>
<td>Exponent notation. Prints the number in scientific
notation using the letter &#8216;e&#8217; to indicate the exponent.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'E'</span></tt></td>
<td>Exponent notation. Same as <tt class="docutils literal"><span class="pre">'e'</span></tt> except it uses an
upper case &#8216;E&#8217; as the separator character.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'f'</span></tt></td>
<td>Fixed point. Displays the number as a fixed-point
number.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'F'</span></tt></td>
<td>Fixed point. Same as <tt class="docutils literal"><span class="pre">'f'</span></tt>.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'g'</span></tt></td>
<td><p class="first">General format.  For a given precision <tt class="docutils literal"><span class="pre">p</span> <span class="pre">&gt;=</span> <span class="pre">1</span></tt>,
this rounds the number to <tt class="docutils literal"><span class="pre">p</span></tt> significant digits and
then formats the result in either fixed-point format
or in scientific notation, depending on its magnitude.</p>
<p>The precise rules are as follows: suppose that the
result formatted with presentation type <tt class="docutils literal"><span class="pre">'e'</span></tt> and
precision <tt class="docutils literal"><span class="pre">p-1</span></tt> would have exponent <tt class="docutils literal"><span class="pre">exp</span></tt>.  Then
if <tt class="docutils literal"><span class="pre">-4</span> <span class="pre">&lt;=</span> <span class="pre">exp</span> <span class="pre">&lt;</span> <span class="pre">p</span></tt>, the number is formatted
with presentation type <tt class="docutils literal"><span class="pre">'f'</span></tt> and precision
<tt class="docutils literal"><span class="pre">p-1-exp</span></tt>.  Otherwise, the number is formatted
with presentation type <tt class="docutils literal"><span class="pre">'e'</span></tt> and precision <tt class="docutils literal"><span class="pre">p-1</span></tt>.
In both cases insignificant trailing zeros are removed
from the significand, and the decimal point is also
removed if there are no remaining digits following it.</p>
<p>Postive and negative infinity, positive and negative
zero, and nans, are formatted as <tt class="docutils literal"><span class="pre">inf</span></tt>, <tt class="docutils literal"><span class="pre">-inf</span></tt>,
<tt class="docutils literal"><span class="pre">0</span></tt>, <tt class="docutils literal"><span class="pre">-0</span></tt> and <tt class="docutils literal"><span class="pre">nan</span></tt> respectively, regardless of
the precision.</p>
<p class="last">A precision of <tt class="docutils literal"><span class="pre">0</span></tt> is treated as equivalent to a
precision of <tt class="docutils literal"><span class="pre">1</span></tt>.</p>
</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'G'</span></tt></td>
<td>General format. Same as <tt class="docutils literal"><span class="pre">'g'</span></tt> except switches to
<tt class="docutils literal"><span class="pre">'E'</span></tt> if the number gets too large. The
representations of infinity and NaN are uppercased, too.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'n'</span></tt></td>
<td>Number. This is the same as <tt class="docutils literal"><span class="pre">'g'</span></tt>, except that it uses
the current locale setting to insert the appropriate
number separator characters.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">'%'</span></tt></td>
<td>Percentage. Multiplies the number by 100 and displays
in fixed (<tt class="docutils literal"><span class="pre">'f'</span></tt>) format, followed by a percent sign.</td>
</tr>
<tr><td>None</td>
<td>The same as <tt class="docutils literal"><span class="pre">'g'</span></tt>.</td>
</tr>
</tbody>
</table>
</blockquote>
</div>
</div>
<div class="section" id="template-strings">
<h2>8.1.4. Template strings<a class="headerlink" href="#template-strings" title="Permalink to this headline">¶</a></h2>
<p>Templates provide simpler string substitutions as described in <span class="target" id="index-622"></span><a class="reference external" href="http://www.python.org/dev/peps/pep-0292"><strong>PEP 292</strong></a>.
Instead of the normal <tt class="docutils literal"><span class="pre">%</span></tt>-based substitutions, Templates support <tt class="docutils literal"><span class="pre">$</span></tt>-based substitutions, using the following rules:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">$$</span></tt> is an escape; it is replaced with a single <tt class="docutils literal"><span class="pre">$</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">$identifier</span></tt> names a substitution placeholder matching a mapping key of
<tt class="docutils literal"><span class="pre">&quot;identifier&quot;</span></tt>.  By default, <tt class="docutils literal"><span class="pre">&quot;identifier&quot;</span></tt> must spell a Python
identifier.  The first non-identifier character after the <tt class="docutils literal"><span class="pre">$</span></tt> character
terminates this placeholder specification.</li>
<li><tt class="docutils literal"><span class="pre">${identifier}</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">$identifier</span></tt>.  It is required when valid
identifier characters follow the placeholder but are not part of the
placeholder, such as <tt class="docutils literal"><span class="pre">&quot;${noun}ification&quot;</span></tt>.</li>
</ul>
<p>Any other appearance of <tt class="docutils literal"><span class="pre">$</span></tt> in the string will result in a <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a>
being raised.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.4.</span></p>
<p>The <tt class="xref docutils literal"><span class="pre">string</span></tt> module provides a <a title="string.Template" class="reference internal" href="#string.Template"><tt class="xref docutils literal"><span class="pre">Template</span></tt></a> class that implements
these rules.  The methods of <a title="string.Template" class="reference internal" href="#string.Template"><tt class="xref docutils literal"><span class="pre">Template</span></tt></a> are:</p>
<dl class="class">
<dt id="string.Template">
<em class="property">class </em><tt class="descclassname">string.</tt><tt class="descname">Template</tt><big>(</big><em>template</em><big>)</big><a class="headerlink" href="#string.Template" title="Permalink to this definition">¶</a></dt>
<dd><p>The constructor takes a single argument which is the template string.</p>
<dl class="method">
<dt id="string.Template.substitute">
<tt class="descname">substitute</tt><big>(</big><em>mapping</em><span class="optional">[</span>, <em>**kws</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.Template.substitute" title="Permalink to this definition">¶</a></dt>
<dd>Performs the template substitution, returning a new string.  <em>mapping</em> is
any dictionary-like object with keys that match the placeholders in the
template.  Alternatively, you can provide keyword arguments, where the
keywords are the placeholders.  When both <em>mapping</em> and <em>kws</em> are given
and there are duplicates, the placeholders from <em>kws</em> take precedence.</dd></dl>

<dl class="method">
<dt id="string.Template.safe_substitute">
<tt class="descname">safe_substitute</tt><big>(</big><em>mapping</em><span class="optional">[</span>, <em>**kws</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.Template.safe_substitute" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a title="string.Template.substitute" class="reference internal" href="#string.Template.substitute"><tt class="xref docutils literal"><span class="pre">substitute()</span></tt></a>, except that if placeholders are missing from
<em>mapping</em> and <em>kws</em>, instead of raising a <a title="exceptions.KeyError" class="reference external" href="exceptions.html#exceptions.KeyError"><tt class="xref docutils literal"><span class="pre">KeyError</span></tt></a> exception, the
original placeholder will appear in the resulting string intact.  Also,
unlike with <a title="string.Template.substitute" class="reference internal" href="#string.Template.substitute"><tt class="xref docutils literal"><span class="pre">substitute()</span></tt></a>, any other appearances of the <tt class="docutils literal"><span class="pre">$</span></tt> will
simply return <tt class="docutils literal"><span class="pre">$</span></tt> instead of raising <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a>.</p>
<p>While other exceptions may still occur, this method is called &#8220;safe&#8221;
because substitutions always tries to return a usable string instead of
raising an exception.  In another sense, <a title="string.Template.safe_substitute" class="reference internal" href="#string.Template.safe_substitute"><tt class="xref docutils literal"><span class="pre">safe_substitute()</span></tt></a> may be
anything other than safe, since it will silently ignore malformed
templates containing dangling delimiters, unmatched braces, or
placeholders that are not valid Python identifiers.</p>
</dd></dl>

</dd></dl>

<p><a title="string.Template" class="reference internal" href="#string.Template"><tt class="xref docutils literal"><span class="pre">Template</span></tt></a> instances also provide one public data attribute:</p>
<dl class="attribute">
<dt id="string.string.template">
<tt class="descclassname">string.</tt><tt class="descname">template</tt><a class="headerlink" href="#string.string.template" title="Permalink to this definition">¶</a></dt>
<dd>This is the object passed to the constructor&#8217;s <em>template</em> argument.  In general,
you shouldn&#8217;t change it, but read-only access is not enforced.</dd></dl>

<p>Here is an example of how to use a Template:</p>
<div class="highlight-python"><pre>&gt;&gt;&gt; from string import Template
&gt;&gt;&gt; s = Template('$who likes $what')
&gt;&gt;&gt; s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
&gt;&gt;&gt; d = dict(who='tim')
&gt;&gt;&gt; Template('Give $who $100').substitute(d)
Traceback (most recent call last):
[...]
ValueError: Invalid placeholder in string: line 1, col 10
&gt;&gt;&gt; Template('$who likes $what').substitute(d)
Traceback (most recent call last):
[...]
KeyError: 'what'
&gt;&gt;&gt; Template('$who likes $what').safe_substitute(d)
'tim likes $what'</pre>
</div>
<p>Advanced usage: you can derive subclasses of <a title="string.Template" class="reference internal" href="#string.Template"><tt class="xref docutils literal"><span class="pre">Template</span></tt></a> to customize the
placeholder syntax, delimiter character, or the entire regular expression used
to parse template strings.  To do this, you can override these class attributes:</p>
<ul class="simple">
<li><em>delimiter</em> &#8211; This is the literal string describing a placeholder introducing
delimiter.  The default value <tt class="docutils literal"><span class="pre">$</span></tt>.  Note that this should <em>not</em> be a regular
expression, as the implementation will call <a title="re.escape" class="reference external" href="re.html#re.escape"><tt class="xref docutils literal"><span class="pre">re.escape()</span></tt></a> on this string as
needed.</li>
<li><em>idpattern</em> &#8211; This is the regular expression describing the pattern for
non-braced placeholders (the braces will be added automatically as
appropriate).  The default value is the regular expression
<tt class="docutils literal"><span class="pre">[_a-z][_a-z0-9]*</span></tt>.</li>
</ul>
<p>Alternatively, you can provide the entire regular expression pattern by
overriding the class attribute <em>pattern</em>.  If you do this, the value must be a
regular expression object with four named capturing groups.  The capturing
groups correspond to the rules given above, along with the invalid placeholder
rule:</p>
<ul class="simple">
<li><em>escaped</em> &#8211; This group matches the escape sequence, e.g. <tt class="docutils literal"><span class="pre">$$</span></tt>, in the
default pattern.</li>
<li><em>named</em> &#8211; This group matches the unbraced placeholder name; it should not
include the delimiter in capturing group.</li>
<li><em>braced</em> &#8211; This group matches the brace enclosed placeholder name; it should
not include either the delimiter or braces in the capturing group.</li>
<li><em>invalid</em> &#8211; This group matches any other delimiter pattern (usually a single
delimiter), and it should appear last in the regular expression.</li>
</ul>
</div>
<div class="section" id="string-functions">
<h2>8.1.5. String functions<a class="headerlink" href="#string-functions" title="Permalink to this headline">¶</a></h2>
<p>The following functions are available to operate on string and Unicode objects.
They are not available as string methods.</p>
<dl class="function">
<dt id="string.capwords">
<tt class="descclassname">string.</tt><tt class="descname">capwords</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.capwords" title="Permalink to this definition">¶</a></dt>
<dd>Split the argument into words using <a title="str.split" class="reference external" href="stdtypes.html#str.split"><tt class="xref docutils literal"><span class="pre">str.split()</span></tt></a>, capitalize each word
using <a title="str.capitalize" class="reference external" href="stdtypes.html#str.capitalize"><tt class="xref docutils literal"><span class="pre">str.capitalize()</span></tt></a>, and join the capitalized words using
<a title="str.join" class="reference external" href="stdtypes.html#str.join"><tt class="xref docutils literal"><span class="pre">str.join()</span></tt></a>.  If the optional second argument <em>sep</em> is absent
or <tt class="xref docutils literal"><span class="pre">None</span></tt>, runs of whitespace characters are replaced by a single space
and leading and trailing whitespace are removed, otherwise <em>sep</em> is used to
split and join the words.</dd></dl>

<dl class="function">
<dt id="string.maketrans">
<tt class="descclassname">string.</tt><tt class="descname">maketrans</tt><big>(</big><em>from</em>, <em>to</em><big>)</big><a class="headerlink" href="#string.maketrans" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a translation table suitable for passing to <a title="string.translate" class="reference internal" href="#string.translate"><tt class="xref docutils literal"><span class="pre">translate()</span></tt></a>, that will
map each character in <em>from</em> into the character at the same position in <em>to</em>;
<em>from</em> and <em>to</em> must have the same length.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Don&#8217;t use strings derived from <a title="string.lowercase" class="reference internal" href="#string.lowercase"><tt class="xref docutils literal"><span class="pre">lowercase</span></tt></a> and <a title="string.uppercase" class="reference internal" href="#string.uppercase"><tt class="xref docutils literal"><span class="pre">uppercase</span></tt></a> as
arguments; in some locales, these don&#8217;t have the same length.  For case
conversions, always use <a title="str.lower" class="reference external" href="stdtypes.html#str.lower"><tt class="xref docutils literal"><span class="pre">str.lower()</span></tt></a> and <a title="str.upper" class="reference external" href="stdtypes.html#str.upper"><tt class="xref docutils literal"><span class="pre">str.upper()</span></tt></a>.</p>
</div>
</dd></dl>

</div>
<div class="section" id="deprecated-string-functions">
<h2>8.1.6. Deprecated string functions<a class="headerlink" href="#deprecated-string-functions" title="Permalink to this headline">¶</a></h2>
<p>The following list of functions are also defined as methods of string and
Unicode objects; see section <a class="reference external" href="stdtypes.html#string-methods"><em>String Methods</em></a> for more information on
those.  You should consider these functions as deprecated, although they will
not be removed until Python 3.0.  The functions defined in this module are:</p>
<dl class="function">
<dt id="string.atof">
<tt class="descclassname">string.</tt><tt class="descname">atof</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#string.atof" title="Permalink to this definition">¶</a></dt>
<dd><p class="deprecated">
<span class="versionmodified">Deprecated since version 2.0: </span>Use the <a title="float" class="reference external" href="functions.html#float"><tt class="xref docutils literal"><span class="pre">float()</span></tt></a> built-in function.</p>
<p id="index-623">Convert a string to a floating point number.  The string must have the standard
syntax for a floating point literal in Python, optionally preceded by a sign
(<tt class="docutils literal"><span class="pre">+</span></tt> or <tt class="docutils literal"><span class="pre">-</span></tt>).  Note that this behaves identical to the built-in function
<a title="float" class="reference external" href="functions.html#float"><tt class="xref docutils literal"><span class="pre">float()</span></tt></a> when passed a string.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last" id="index-624">When passing in a string, values for NaN and Infinity may be returned, depending
on the underlying C library.  The specific set of strings accepted which cause
these values to be returned depends entirely on the C library and is known to
vary.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="string.atoi">
<tt class="descclassname">string.</tt><tt class="descname">atoi</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>base</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.atoi" title="Permalink to this definition">¶</a></dt>
<dd><p class="deprecated">
<span class="versionmodified">Deprecated since version 2.0: </span>Use the <a title="int" class="reference external" href="functions.html#int"><tt class="xref docutils literal"><span class="pre">int()</span></tt></a> built-in function.</p>
<p id="index-625">Convert string <em>s</em> to an integer in the given <em>base</em>.  The string must consist
of one or more digits, optionally preceded by a sign (<tt class="docutils literal"><span class="pre">+</span></tt> or <tt class="docutils literal"><span class="pre">-</span></tt>).  The
<em>base</em> defaults to 10.  If it is 0, a default base is chosen depending on the
leading characters of the string (after stripping the sign): <tt class="docutils literal"><span class="pre">0x</span></tt> or <tt class="docutils literal"><span class="pre">0X</span></tt>
means 16, <tt class="docutils literal"><span class="pre">0</span></tt> means 8, anything else means 10.  If <em>base</em> is 16, a leading
<tt class="docutils literal"><span class="pre">0x</span></tt> or <tt class="docutils literal"><span class="pre">0X</span></tt> is always accepted, though not required.  This behaves
identically to the built-in function <a title="int" class="reference external" href="functions.html#int"><tt class="xref docutils literal"><span class="pre">int()</span></tt></a> when passed a string.  (Also
note: for a more flexible interpretation of numeric literals, use the built-in
function <a title="eval" class="reference external" href="functions.html#eval"><tt class="xref docutils literal"><span class="pre">eval()</span></tt></a>.)</p>
</dd></dl>

<dl class="function">
<dt id="string.atol">
<tt class="descclassname">string.</tt><tt class="descname">atol</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>base</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.atol" title="Permalink to this definition">¶</a></dt>
<dd><p class="deprecated">
<span class="versionmodified">Deprecated since version 2.0: </span>Use the <a title="long" class="reference external" href="functions.html#long"><tt class="xref docutils literal"><span class="pre">long()</span></tt></a> built-in function.</p>
<p id="index-626">Convert string <em>s</em> to a long integer in the given <em>base</em>. The string must
consist of one or more digits, optionally preceded by a sign (<tt class="docutils literal"><span class="pre">+</span></tt> or <tt class="docutils literal"><span class="pre">-</span></tt>).
The <em>base</em> argument has the same meaning as for <a title="string.atoi" class="reference internal" href="#string.atoi"><tt class="xref docutils literal"><span class="pre">atoi()</span></tt></a>.  A trailing <tt class="docutils literal"><span class="pre">l</span></tt>
or <tt class="docutils literal"><span class="pre">L</span></tt> is not allowed, except if the base is 0.  Note that when invoked
without <em>base</em> or with <em>base</em> set to 10, this behaves identical to the built-in
function <a title="long" class="reference external" href="functions.html#long"><tt class="xref docutils literal"><span class="pre">long()</span></tt></a> when passed a string.</p>
</dd></dl>

<dl class="function">
<dt id="string.capitalize">
<tt class="descclassname">string.</tt><tt class="descname">capitalize</tt><big>(</big><em>word</em><big>)</big><a class="headerlink" href="#string.capitalize" title="Permalink to this definition">¶</a></dt>
<dd>Return a copy of <em>word</em> with only its first character capitalized.</dd></dl>

<dl class="function">
<dt id="string.expandtabs">
<tt class="descclassname">string.</tt><tt class="descname">expandtabs</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>tabsize</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.expandtabs" title="Permalink to this definition">¶</a></dt>
<dd>Expand tabs in a string replacing them by one or more spaces, depending on the
current column and the given tab size.  The column number is reset to zero after
each newline occurring in the string. This doesn&#8217;t understand other non-printing
characters or escape sequences.  The tab size defaults to 8.</dd></dl>

<dl class="function">
<dt id="string.find">
<tt class="descclassname">string.</tt><tt class="descname">find</tt><big>(</big><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.find" title="Permalink to this definition">¶</a></dt>
<dd>Return the lowest index in <em>s</em> where the substring <em>sub</em> is found such that
<em>sub</em> is wholly contained in <tt class="docutils literal"><span class="pre">s[start:end]</span></tt>.  Return <tt class="docutils literal"><span class="pre">-1</span></tt> on failure.
Defaults for <em>start</em> and <em>end</em> and interpretation of negative values is the same
as for slices.</dd></dl>

<dl class="function">
<dt id="string.rfind">
<tt class="descclassname">string.</tt><tt class="descname">rfind</tt><big>(</big><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.rfind" title="Permalink to this definition">¶</a></dt>
<dd>Like <a title="string.find" class="reference internal" href="#string.find"><tt class="xref docutils literal"><span class="pre">find()</span></tt></a> but find the highest index.</dd></dl>

<dl class="function">
<dt id="string.index">
<tt class="descclassname">string.</tt><tt class="descname">index</tt><big>(</big><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.index" title="Permalink to this definition">¶</a></dt>
<dd>Like <a title="string.find" class="reference internal" href="#string.find"><tt class="xref docutils literal"><span class="pre">find()</span></tt></a> but raise <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> when the substring is not found.</dd></dl>

<dl class="function">
<dt id="string.rindex">
<tt class="descclassname">string.</tt><tt class="descname">rindex</tt><big>(</big><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.rindex" title="Permalink to this definition">¶</a></dt>
<dd>Like <a title="string.rfind" class="reference internal" href="#string.rfind"><tt class="xref docutils literal"><span class="pre">rfind()</span></tt></a> but raise <a title="exceptions.ValueError" class="reference external" href="exceptions.html#exceptions.ValueError"><tt class="xref docutils literal"><span class="pre">ValueError</span></tt></a> when the substring is not found.</dd></dl>

<dl class="function">
<dt id="string.count">
<tt class="descclassname">string.</tt><tt class="descname">count</tt><big>(</big><em>s</em>, <em>sub</em><span class="optional">[</span>, <em>start</em><span class="optional">[</span>, <em>end</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.count" title="Permalink to this definition">¶</a></dt>
<dd>Return the number of (non-overlapping) occurrences of substring <em>sub</em> in string
<tt class="docutils literal"><span class="pre">s[start:end]</span></tt>. Defaults for <em>start</em> and <em>end</em> and interpretation of negative
values are the same as for slices.</dd></dl>

<dl class="function">
<dt id="string.lower">
<tt class="descclassname">string.</tt><tt class="descname">lower</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#string.lower" title="Permalink to this definition">¶</a></dt>
<dd>Return a copy of <em>s</em>, but with upper case letters converted to lower case.</dd></dl>

<dl class="function">
<dt id="string.split">
<tt class="descclassname">string.</tt><tt class="descname">split</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">[</span>, <em>maxsplit</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.split" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the words of the string <em>s</em>.  If the optional second argument
<em>sep</em> is absent or <tt class="xref docutils literal"><span class="pre">None</span></tt>, the words are separated by arbitrary strings of
whitespace characters (space, tab,  newline, return, formfeed).  If the second
argument <em>sep</em> is present and not <tt class="xref docutils literal"><span class="pre">None</span></tt>, it specifies a string to be used as
the  word separator.  The returned list will then have one more item than the
number of non-overlapping occurrences of the separator in the string.  The
optional third argument <em>maxsplit</em> defaults to 0.  If it is nonzero, at most
<em>maxsplit</em> number of splits occur, and the remainder of the string is returned
as the final element of the list (thus, the list will have at most
<tt class="docutils literal"><span class="pre">maxsplit+1</span></tt> elements).</p>
<p>The behavior of split on an empty string depends on the value of <em>sep</em>. If <em>sep</em>
is not specified, or specified as <tt class="xref docutils literal"><span class="pre">None</span></tt>, the result will be an empty list.
If <em>sep</em> is specified as any string, the result will be a list containing one
element which is an empty string.</p>
</dd></dl>

<dl class="function">
<dt id="string.rsplit">
<tt class="descclassname">string.</tt><tt class="descname">rsplit</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">[</span>, <em>maxsplit</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.rsplit" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the words of the string <em>s</em>, scanning <em>s</em> from the end.  To all
intents and purposes, the resulting list of words is the same as returned by
<a title="string.split" class="reference internal" href="#string.split"><tt class="xref docutils literal"><span class="pre">split()</span></tt></a>, except when the optional third argument <em>maxsplit</em> is explicitly
specified and nonzero.  When <em>maxsplit</em> is nonzero, at most <em>maxsplit</em> number of
splits &#8211; the <em>rightmost</em> ones &#8211; occur, and the remainder of the string is
returned as the first element of the list (thus, the list will have at most
<tt class="docutils literal"><span class="pre">maxsplit+1</span></tt> elements).</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.4.</span></p>
</dd></dl>

<dl class="function">
<dt id="string.splitfields">
<tt class="descclassname">string.</tt><tt class="descname">splitfields</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>sep</em><span class="optional">[</span>, <em>maxsplit</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.splitfields" title="Permalink to this definition">¶</a></dt>
<dd>This function behaves identically to <a title="string.split" class="reference internal" href="#string.split"><tt class="xref docutils literal"><span class="pre">split()</span></tt></a>.  (In the past, <a title="string.split" class="reference internal" href="#string.split"><tt class="xref docutils literal"><span class="pre">split()</span></tt></a>
was only used with one argument, while <a title="string.splitfields" class="reference internal" href="#string.splitfields"><tt class="xref docutils literal"><span class="pre">splitfields()</span></tt></a> was only used with
two arguments.)</dd></dl>

<dl class="function">
<dt id="string.join">
<tt class="descclassname">string.</tt><tt class="descname">join</tt><big>(</big><em>words</em><span class="optional">[</span>, <em>sep</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.join" title="Permalink to this definition">¶</a></dt>
<dd>Concatenate a list or tuple of words with intervening occurrences of  <em>sep</em>.
The default value for <em>sep</em> is a single space character.  It is always true that
<tt class="docutils literal"><span class="pre">string.join(string.split(s,</span> <span class="pre">sep),</span> <span class="pre">sep)</span></tt> equals <em>s</em>.</dd></dl>

<dl class="function">
<dt id="string.joinfields">
<tt class="descclassname">string.</tt><tt class="descname">joinfields</tt><big>(</big><em>words</em><span class="optional">[</span>, <em>sep</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.joinfields" title="Permalink to this definition">¶</a></dt>
<dd>This function behaves identically to <a title="string.join" class="reference internal" href="#string.join"><tt class="xref docutils literal"><span class="pre">join()</span></tt></a>.  (In the past,  <a title="string.join" class="reference internal" href="#string.join"><tt class="xref docutils literal"><span class="pre">join()</span></tt></a>
was only used with one argument, while <a title="string.joinfields" class="reference internal" href="#string.joinfields"><tt class="xref docutils literal"><span class="pre">joinfields()</span></tt></a> was only used with two
arguments.) Note that there is no <a title="string.joinfields" class="reference internal" href="#string.joinfields"><tt class="xref docutils literal"><span class="pre">joinfields()</span></tt></a> method on string objects;
use the <a title="string.join" class="reference internal" href="#string.join"><tt class="xref docutils literal"><span class="pre">join()</span></tt></a> method instead.</dd></dl>

<dl class="function">
<dt id="string.lstrip">
<tt class="descclassname">string.</tt><tt class="descname">lstrip</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>chars</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.lstrip" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the string with leading characters removed.  If <em>chars</em> is
omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, whitespace characters are removed.  If given and not
<tt class="xref docutils literal"><span class="pre">None</span></tt>, <em>chars</em> must be a string; the characters in the string will be
stripped from the beginning of the string this method is called on.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.2.3: </span>The <em>chars</em> parameter was added.  The <em>chars</em> parameter cannot be passed in
earlier 2.2 versions.</p>
</dd></dl>

<dl class="function">
<dt id="string.rstrip">
<tt class="descclassname">string.</tt><tt class="descname">rstrip</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>chars</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.rstrip" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the string with trailing characters removed.  If <em>chars</em> is
omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, whitespace characters are removed.  If given and not
<tt class="xref docutils literal"><span class="pre">None</span></tt>, <em>chars</em> must be a string; the characters in the string will be
stripped from the end of the string this method is called on.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.2.3: </span>The <em>chars</em> parameter was added.  The <em>chars</em> parameter cannot be passed in
earlier 2.2 versions.</p>
</dd></dl>

<dl class="function">
<dt id="string.strip">
<tt class="descclassname">string.</tt><tt class="descname">strip</tt><big>(</big><em>s</em><span class="optional">[</span>, <em>chars</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.strip" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the string with leading and trailing characters removed.  If
<em>chars</em> is omitted or <tt class="xref docutils literal"><span class="pre">None</span></tt>, whitespace characters are removed.  If given and
not <tt class="xref docutils literal"><span class="pre">None</span></tt>, <em>chars</em> must be a string; the characters in the string will be
stripped from the both ends of the string this method is called on.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.2.3: </span>The <em>chars</em> parameter was added.  The <em>chars</em> parameter cannot be passed in
earlier 2.2 versions.</p>
</dd></dl>

<dl class="function">
<dt id="string.swapcase">
<tt class="descclassname">string.</tt><tt class="descname">swapcase</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#string.swapcase" title="Permalink to this definition">¶</a></dt>
<dd>Return a copy of <em>s</em>, but with lower case letters converted to upper case and
vice versa.</dd></dl>

<dl class="function">
<dt id="string.translate">
<tt class="descclassname">string.</tt><tt class="descname">translate</tt><big>(</big><em>s</em>, <em>table</em><span class="optional">[</span>, <em>deletechars</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.translate" title="Permalink to this definition">¶</a></dt>
<dd>Delete all characters from <em>s</em> that are in <em>deletechars</em> (if  present), and then
translate the characters using <em>table</em>, which  must be a 256-character string
giving the translation for each character value, indexed by its ordinal.  If
<em>table</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt>, then only the character deletion step is performed.</dd></dl>

<dl class="function">
<dt id="string.upper">
<tt class="descclassname">string.</tt><tt class="descname">upper</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#string.upper" title="Permalink to this definition">¶</a></dt>
<dd>Return a copy of <em>s</em>, but with lower case letters converted to upper case.</dd></dl>

<dl class="function">
<dt id="string.ljust">
<tt class="descclassname">string.</tt><tt class="descname">ljust</tt><big>(</big><em>s</em>, <em>width</em><span class="optional">[</span>, <em>fillchar</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.ljust" title="Permalink to this definition">¶</a></dt>
<dt id="string.rjust">
<tt class="descclassname">string.</tt><tt class="descname">rjust</tt><big>(</big><em>s</em>, <em>width</em><span class="optional">[</span>, <em>fillchar</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.rjust" title="Permalink to this definition">¶</a></dt>
<dt id="string.center">
<tt class="descclassname">string.</tt><tt class="descname">center</tt><big>(</big><em>s</em>, <em>width</em><span class="optional">[</span>, <em>fillchar</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.center" title="Permalink to this definition">¶</a></dt>
<dd>These functions respectively left-justify, right-justify and center a string in
a field of given width.  They return a string that is at least <em>width</em>
characters wide, created by padding the string <em>s</em> with the character <em>fillchar</em>
(default is a space) until the given width on the right, left or both sides.
The string is never truncated.</dd></dl>

<dl class="function">
<dt id="string.zfill">
<tt class="descclassname">string.</tt><tt class="descname">zfill</tt><big>(</big><em>s</em>, <em>width</em><big>)</big><a class="headerlink" href="#string.zfill" title="Permalink to this definition">¶</a></dt>
<dd>Pad a numeric string on the left with zero digits until the given width is
reached.  Strings starting with a sign are handled correctly.</dd></dl>

<dl class="function">
<dt id="string.replace">
<tt class="descclassname">string.</tt><tt class="descname">replace</tt><big>(</big><em>str</em>, <em>old</em>, <em>new</em><span class="optional">[</span>, <em>maxreplace</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#string.replace" title="Permalink to this definition">¶</a></dt>
<dd>Return a copy of string <em>str</em> with all occurrences of substring <em>old</em> replaced
by <em>new</em>.  If the optional argument <em>maxreplace</em> is given, the first
<em>maxreplace</em> occurrences are replaced.</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="#">8.1. <tt class="docutils literal"><span class="pre">string</span></tt> &#8212; Common string operations</a><ul>
<li><a class="reference external" href="#string-constants">8.1.1. String constants</a></li>
<li><a class="reference external" href="#string-formatting">8.1.2. String Formatting</a></li>
<li><a class="reference external" href="#format-string-syntax">8.1.3. Format String Syntax</a><ul>
<li><a class="reference external" href="#format-specification-mini-language">8.1.3.1. Format Specification Mini-Language</a></li>
</ul>
</li>
<li><a class="reference external" href="#template-strings">8.1.4. Template strings</a></li>
<li><a class="reference external" href="#string-functions">8.1.5. String functions</a></li>
<li><a class="reference external" href="#deprecated-string-functions">8.1.6. Deprecated string functions</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="strings.html"
                                  title="previous chapter">8. String Services</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="re.html"
                                  title="next chapter">8.2. <tt class="docutils literal docutils literal"><span class="pre">re</span></tt> &#8212; Regular expression operations</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/string.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="re.html" title="8.2. re — Regular expression operations"
             >next</a> |</li>
        <li class="right" >
          <a href="strings.html" title="8. String Services"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.5 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" >8. String Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2010, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Mar 19, 2010.
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.5.
    </div>

  </body>
</html>