<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>2.1.5.1 More String Operations </TITLE>
<META NAME="description" CONTENT="2.1.5.1 More String Operations ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="typesseq-mutable.html">
<LINK REL="previous" HREF="typesseq.html">
<LINK REL="up" HREF="typesseq.html">
<LINK REL="next" HREF="typesseq-mutable.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="typesseq-mutable.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="typesseq.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="typesseq.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="typesseq-mutable.html">2.1.5.2 Mutable Sequence Types</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="typesseq.html">2.1.5 Sequence Types</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="typesseq.html">2.1.5 Sequence Types</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H3><A NAME="SECTION004151000000000000000"></A>
<BR>
2.1.5.1 More String Operations 
</H3>

<P>
String objects have one unique built-in operation: the <tt>%</tt>operator (modulo) with a string left argument interprets this string
as a C <tt class=cfunction>sprintf()</tt> format string to be applied to the
right argument, and returns the string resulting from this formatting
operation.

<P>
The right argument should be a tuple with one item for each argument
required by the format string; if the string requires a single
argument, the right argument may also be a single non-tuple
object.<A NAME="tex2html4"
 HREF="#foot605"><SUP>2.4</SUP></A>The following format characters are understood:
<tt>%</tt>, <tt>c</tt>, <tt>s</tt>, <tt>i</tt>, <tt>d</tt>, <tt>u</tt>, <tt>o</tt>,
<tt>x</tt>, <tt>X</tt>, <tt>e</tt>, <tt>E</tt>, <tt>f</tt>, <tt>g</tt>, <tt>G</tt>. 
Width and precision may be a <tt>*</tt> to specify that an integer argument
specifies the actual width or precision.  The flag characters
<tt>-</tt>, <tt>+</tt>, blank, <tt>#</tt> and <tt>0</tt> are understood.  The 
size specifiers <tt>h</tt>, <tt>l</tt> or <tt>L</tt> may be present but are
ignored.  The <tt>%s</tt> conversion takes any Python object and
converts it to a string using <tt>str()</tt> before formatting it.  The
ANSI features <tt>%p</tt> and <tt>%n</tt> are not supported.  Since
Python strings have an explicit length, <tt>%s</tt> conversions don't
assume that <tt>'&#92;0'</tt> is the end of the string.

<P>
For safety reasons, floating point precisions are clipped to 50;
<tt>%f</tt> conversions for numbers whose absolute value is over 1e25
are replaced by <tt>%g</tt> conversions.<A NAME="tex2html5"
 HREF="#foot636"><SUP>2.5</SUP></A>All other errors raise exceptions.

<P>
If the right argument is a dictionary (or any kind of mapping), then
the formats in the string must have a parenthesized key into that
dictionary inserted immediately after the "<tt class=character>%</tt>" character,
and each format formats the corresponding entry from the mapping.
For example:

<P>
<dl><dd><pre class=verbatim>
&gt;&gt;&gt; count = 2
&gt;&gt;&gt; language = 'Python'
&gt;&gt;&gt; print'%(language)s has %(count)03d quote types.' % vars()
Python has 002 quote types.
</pre></dl>

<P>
In this case no <tt>*</tt> specifiers may occur in a format (since they
require a sequential parameter list).

<P>
Additional string operations are defined in standard module
<tt class=module>string</tt> and in built-in module <tt class=module>re</tt>.
<a name="l2h-82"></a>
<a name="l2h-83"></a>

<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot605">...
object.</A><A NAME="foot605"
 HREF="typesseq-strings.html#tex2html4"><SUP>2.4</SUP></A>
<DD>A tuple object in this case should be a singleton.

<DT><A NAME="foot636">... conversions.</A><A NAME="foot636"
 HREF="typesseq-strings.html#tex2html5"><SUP>2.5</SUP></A>
<DD>
  These numbers are fairly arbitrary.  They are intended to
  avoid printing endless strings of meaningless digits without hampering
  correct use and without having to know the exact precision of floating
  point values on a particular machine.

</DL><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="typesseq-mutable.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="typesseq.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="typesseq.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="typesseq-mutable.html">2.1.5.2 Mutable Sequence Types</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="typesseq.html">2.1.5 Sequence Types</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="typesseq.html">2.1.5 Sequence Types</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
