<!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>String conversion and formatting — 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="Utilities" href="utilities.html" /> <link rel="next" title="Reflection" href="reflection.html" /> <link rel="prev" title="Parsing arguments and building values" href="arg.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="reflection.html" title="Reflection" accesskey="N">next</a> |</li> <li class="right" > <a href="arg.html" title="Parsing arguments and building values" 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> »</li> <li><a href="index.html" >Python/C API Reference Manual</a> »</li> <li><a href="utilities.html" accesskey="U">Utilities</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="string-conversion-and-formatting"> <span id="string-conversion"></span><h1>String conversion and formatting<a class="headerlink" href="#string-conversion-and-formatting" title="Permalink to this headline">¶</a></h1> <p>Functions for number conversion and formatted string output.</p> <dl class="cfunction"> <dt id="PyOS_snprintf"> int <tt class="descname">PyOS_snprintf</tt><big>(</big>char<em> *str</em>, size_t<em> size</em>, const char<em> *format</em>, ...<big>)</big><a class="headerlink" href="#PyOS_snprintf" title="Permalink to this definition">¶</a></dt> <dd>Output not more than <em>size</em> bytes to <em>str</em> according to the format string <em>format</em> and the extra arguments. See the Unix man page <em>snprintf(2)</em>.</dd></dl> <dl class="cfunction"> <dt id="PyOS_vsnprintf"> int <tt class="descname">PyOS_vsnprintf</tt><big>(</big>char<em> *str</em>, size_t<em> size</em>, const char<em> *format</em>, va_list<em> va</em><big>)</big><a class="headerlink" href="#PyOS_vsnprintf" title="Permalink to this definition">¶</a></dt> <dd>Output not more than <em>size</em> bytes to <em>str</em> according to the format string <em>format</em> and the variable argument list <em>va</em>. Unix man page <em>vsnprintf(2)</em>.</dd></dl> <p><a title="PyOS_snprintf" class="reference internal" href="#PyOS_snprintf"><tt class="xref docutils literal"><span class="pre">PyOS_snprintf()</span></tt></a> and <a title="PyOS_vsnprintf" class="reference internal" href="#PyOS_vsnprintf"><tt class="xref docutils literal"><span class="pre">PyOS_vsnprintf()</span></tt></a> wrap the Standard C library functions <tt class="xref docutils literal"><span class="pre">snprintf()</span></tt> and <tt class="xref docutils literal"><span class="pre">vsnprintf()</span></tt>. Their purpose is to guarantee consistent behavior in corner cases, which the Standard C functions do not.</p> <p>The wrappers ensure that <em>str*[*size</em>-1] is always <tt class="docutils literal"><span class="pre">'\0'</span></tt> upon return. They never write more than <em>size</em> bytes (including the trailing <tt class="docutils literal"><span class="pre">'\0'</span></tt> into str. Both functions require that <tt class="docutils literal"><span class="pre">str</span> <span class="pre">!=</span> <span class="pre">NULL</span></tt>, <tt class="docutils literal"><span class="pre">size</span> <span class="pre">></span> <span class="pre">0</span></tt> and <tt class="docutils literal"><span class="pre">format</span> <span class="pre">!=</span> <span class="pre">NULL</span></tt>.</p> <p>If the platform doesn’t have <tt class="xref docutils literal"><span class="pre">vsnprintf()</span></tt> and the buffer size needed to avoid truncation exceeds <em>size</em> by more than 512 bytes, Python aborts with a <em>Py_FatalError</em>.</p> <p>The return value (<em>rv</em>) for these functions should be interpreted as follows:</p> <ul class="simple"> <li>When <tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">rv</span> <span class="pre"><</span> <span class="pre">size</span></tt>, the output conversion was successful and <em>rv</em> characters were written to <em>str</em> (excluding the trailing <tt class="docutils literal"><span class="pre">'\0'</span></tt> byte at <em>str*[*rv</em>]).</li> <li>When <tt class="docutils literal"><span class="pre">rv</span> <span class="pre">>=</span> <span class="pre">size</span></tt>, the output conversion was truncated and a buffer with <tt class="docutils literal"><span class="pre">rv</span> <span class="pre">+</span> <span class="pre">1</span></tt> bytes would have been needed to succeed. <em>str*[*size</em>-1] is <tt class="docutils literal"><span class="pre">'\0'</span></tt> in this case.</li> <li>When <tt class="docutils literal"><span class="pre">rv</span> <span class="pre"><</span> <span class="pre">0</span></tt>, “something bad happened.” <em>str*[*size</em>-1] is <tt class="docutils literal"><span class="pre">'\0'</span></tt> in this case too, but the rest of <em>str</em> is undefined. The exact cause of the error depends on the underlying platform.</li> </ul> <p>The following functions provide locale-independent string to number conversions.</p> <dl class="cfunction"> <dt id="PyOS_ascii_strtod"> double <tt class="descname">PyOS_ascii_strtod</tt><big>(</big>const char<em> *nptr</em>, char<em> **endptr</em><big>)</big><a class="headerlink" href="#PyOS_ascii_strtod" title="Permalink to this definition">¶</a></dt> <dd><p>Convert a string to a <tt class="xref docutils literal"><span class="pre">double</span></tt>. This function behaves like the Standard C function <tt class="xref docutils literal"><span class="pre">strtod()</span></tt> does in the C locale. It does this without changing the current locale, since that would not be thread-safe.</p> <p><a title="PyOS_ascii_strtod" class="reference internal" href="#PyOS_ascii_strtod"><tt class="xref docutils literal"><span class="pre">PyOS_ascii_strtod()</span></tt></a> should typically be used for reading configuration files or other non-user input that should be locale independent.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> <p>See the Unix man page <em>strtod(2)</em> for details.</p> </dd></dl> <dl class="cfunction"> <dt id="PyOS_ascii_formatd"> char * <tt class="descname">PyOS_ascii_formatd</tt><big>(</big>char<em> *buffer</em>, size_t<em> buf_len</em>, const char<em> *format</em>, double<em> d</em><big>)</big><a class="headerlink" href="#PyOS_ascii_formatd" title="Permalink to this definition">¶</a></dt> <dd><p>Convert a <tt class="xref docutils literal"><span class="pre">double</span></tt> to a string using the <tt class="docutils literal"><span class="pre">'.'</span></tt> as the decimal separator. <em>format</em> is a <tt class="xref docutils literal"><span class="pre">printf()</span></tt>-style format string specifying the number format. Allowed conversion characters are <tt class="docutils literal"><span class="pre">'e'</span></tt>, <tt class="docutils literal"><span class="pre">'E'</span></tt>, <tt class="docutils literal"><span class="pre">'f'</span></tt>, <tt class="docutils literal"><span class="pre">'F'</span></tt>, <tt class="docutils literal"><span class="pre">'g'</span></tt> and <tt class="docutils literal"><span class="pre">'G'</span></tt>.</p> <p>The return value is a pointer to <em>buffer</em> with the converted string or NULL if the conversion failed.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> </dd></dl> <dl class="cfunction"> <dt id="PyOS_ascii_atof"> double <tt class="descname">PyOS_ascii_atof</tt><big>(</big>const char<em> *nptr</em><big>)</big><a class="headerlink" href="#PyOS_ascii_atof" title="Permalink to this definition">¶</a></dt> <dd><p>Convert a string to a <tt class="xref docutils literal"><span class="pre">double</span></tt> in a locale-independent way.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> <p>See the Unix man page <em>atof(2)</em> for details.</p> </dd></dl> <dl class="cfunction"> <dt id="PyOS_stricmp"> char * <tt class="descname">PyOS_stricmp</tt><big>(</big>char<em> *s1</em>, char<em> *s2</em><big>)</big><a class="headerlink" href="#PyOS_stricmp" title="Permalink to this definition">¶</a></dt> <dd><p>Case insensitive comparison of strings. The function works almost identically to <tt class="xref docutils literal"><span class="pre">strcmp()</span></tt> except that it ignores the case.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="cfunction"> <dt id="PyOS_strnicmp"> char * <tt class="descname">PyOS_strnicmp</tt><big>(</big>char<em> *s1</em>, char<em> *s2</em>, Py_ssize_t <em> size</em><big>)</big><a class="headerlink" href="#PyOS_strnicmp" title="Permalink to this definition">¶</a></dt> <dd><p>Case insensitive comparison of strings. The function works almost identically to <tt class="xref docutils literal"><span class="pre">strncmp()</span></tt> except that it ignores the case.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="arg.html" title="previous chapter">Parsing arguments and building values</a></p> <h4>Next topic</h4> <p class="topless"><a href="reflection.html" title="next chapter">Reflection</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/c-api/conversion.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="reflection.html" title="Reflection" >next</a> |</li> <li class="right" > <a href="arg.html" title="Parsing arguments and building values" >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> »</li> <li><a href="index.html" >Python/C API Reference Manual</a> »</li> <li><a href="utilities.html" >Utilities</a> »</li> </ul> </div> <div class="footer"> © <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>