MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //usr/share/doc/python-docs-2.4.3/html/api/stringObjects.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="api.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="api.html" title='Python/C API Reference Manual' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="unicodeObjects.html" />
<link rel="prev" href="sequenceObjects.html" />
<link rel="parent" href="sequenceObjects.html" />
<link rel="next" href="unicodeObjects.html" />
<meta name='aesop' content='information' />
<title>7.3.1 String Objects </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.3 Sequence Objects"
  href="sequenceObjects.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="7.3 Sequence Objects"
  href="sequenceObjects.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.3.2 Unicode Objects"
  href="unicodeObjects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="sequenceObjects.html">7.3 Sequence Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="sequenceObjects.html">7.3 Sequence Objects</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="unicodeObjects.html">7.3.2 Unicode Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION009310000000000000000"></A><A NAME="stringObjects"></A>
<BR>
7.3.1 String Objects 
</H2>

<P>
These functions raise <tt class="exception">TypeError</tt> when expecting a string
parameter and are called with a non-string parameter.

<P>
<a id='l2h-447' xml:id='l2h-447'></a><dl><dt><b><tt class="ctype"><a id='l2h-448' xml:id='l2h-448'>PyStringObject</a></tt></b></dt>
<dd>
  This subtype of <tt class="ctype">PyObject</tt> represents a Python string object.
</dl>

<P>
<dl><dt>PyTypeObject <b><tt id='l2h-449' xml:id='l2h-449' class="cdata">PyString_Type</tt></b></dt>
<dd>
  This instance of <tt class="ctype">PyTypeObject</tt> represents the Python string
  type; it is the same object as <code>types.TypeType</code> in the Python
  layer.
  <a id='l2h-451' xml:id='l2h-451'></a>.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int&nbsp;<b><tt id='l2h-452' xml:id='l2h-452' class="cfunction">PyString_Check</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt>
<dd>
  Return true if the object <var>o</var> is a string object or an instance
  of a subtype of the string type.
  
<span class="versionnote">Changed in version 2.2:
Allowed subtypes to be accepted.</span>

</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int&nbsp;<b><tt id='l2h-453' xml:id='l2h-453' class="cfunction">PyString_CheckExact</tt></b>(</nobr></td><td>PyObject *<var>o</var>)</td></tr></table></dt>
<dd>
  Return true if the object <var>o</var> is a string object, but not an
  instance of a subtype of the string type.
  
<span class="versionnote">New in version 2.2.</span>

</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-454' xml:id='l2h-454' class="cfunction">PyString_FromString</tt></b>(</nobr></td><td>const char *<var>v</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Return a new string object with the value <var>v</var> on success, and
  <tt class="constant">NULL</tt> on failure.  The parameter <var>v</var> must not be <tt class="constant">NULL</tt>; it
  will not be checked.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-455' xml:id='l2h-455' class="cfunction">PyString_FromStringAndSize</tt></b>(</nobr></td><td>const char *<var>v</var>,
                                                         int <var>len</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Return a new string object with the value <var>v</var> and length
  <var>len</var> on success, and <tt class="constant">NULL</tt> on failure.  If <var>v</var> is
  <tt class="constant">NULL</tt>, the contents of the string are uninitialized.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-456' xml:id='l2h-456' class="cfunction">PyString_FromFormat</tt></b>(</nobr></td><td>const char *<var>format</var>, ...)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Take a C <tt class="cfunction">printf()</tt>-style <var>format</var> string and a
  variable number of arguments, calculate the size of the resulting
  Python string and return a string with the values formatted into
  it.  The variable arguments must be C types and must correspond
  exactly to the format characters in the <var>format</var> string.  The
  following format characters are allowed:

<P>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="left"  >Format Characters</th>
      <th class="left"  >Type</th>
      <th class="left"  >Comment</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="left"   valign="baseline"><tt class="member">%%</tt></td>
        <td class="left"  ><em>n/a</em></td>
        <td class="left"  >The literal % character.</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="member">%c</tt></td>
        <td class="left"  >int</td>
        <td class="left"  >A single character, represented as an C int.</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="member">%d</tt></td>
        <td class="left"  >int</td>
        <td class="left"  >Exactly equivalent to <code>printf("%d")</code>.</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="member">%ld</tt></td>
        <td class="left"  >long</td>
        <td class="left"  >Exactly equivalent to <code>printf("%ld")</code>.</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="member">%i</tt></td>
        <td class="left"  >int</td>
        <td class="left"  >Exactly equivalent to <code>printf("%i")</code>.</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="member">%x</tt></td>
        <td class="left"  >int</td>
        <td class="left"  >Exactly equivalent to <code>printf("%x")</code>.</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="member">%s</tt></td>
        <td class="left"  >char*</td>
        <td class="left"  >A null-terminated C character array.</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="member">%p</tt></td>
        <td class="left"  >void*</td>
        <td class="left"  >The hex representation of a C pointer.
	Mostly equivalent to <code>printf("%p")</code> except that it is
	guaranteed to start with the literal <code>0x</code> regardless of
	what the platform's <code>printf</code> yields.</td></tr></tbody>
</table></div>
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-457' xml:id='l2h-457' class="cfunction">PyString_FromFormatV</tt></b>(</nobr></td><td>const char *<var>format</var>,
                                                   va_list <var>vargs</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Identical to <tt class="function">PyString_FromFormat()</tt> except that it takes
  exactly two arguments.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int&nbsp;<b><tt id='l2h-458' xml:id='l2h-458' class="cfunction">PyString_Size</tt></b>(</nobr></td><td>PyObject *<var>string</var>)</td></tr></table></dt>
<dd>
  Return the length of the string in string object <var>string</var>.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int&nbsp;<b><tt id='l2h-459' xml:id='l2h-459' class="cfunction">PyString_GET_SIZE</tt></b>(</nobr></td><td>PyObject *<var>string</var>)</td></tr></table></dt>
<dd>
  Macro form of <tt class="cfunction">PyString_Size()</tt> but without error
  checking.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>char*&nbsp;<b><tt id='l2h-460' xml:id='l2h-460' class="cfunction">PyString_AsString</tt></b>(</nobr></td><td>PyObject *<var>string</var>)</td></tr></table></dt>
<dd>
  Return a NUL-terminated representation of the contents of
  <var>string</var>.  The pointer refers to the internal buffer of
  <var>string</var>, not a copy.  The data must not be modified in any way,
  unless the string was just created using
  <code>PyString_FromStringAndSize(NULL, <var>size</var>)</code>.
  It must not be deallocated.  If <var>string</var> is a Unicode object,
  this function computes the default encoding of <var>string</var> and
  operates on that.  If <var>string</var> is not a string object at all,
  <tt class="cfunction">PyString_AsString()</tt> returns <tt class="constant">NULL</tt> and raises
  <tt class="exception">TypeError</tt>.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>char*&nbsp;<b><tt id='l2h-461' xml:id='l2h-461' class="cfunction">PyString_AS_STRING</tt></b>(</nobr></td><td>PyObject *<var>string</var>)</td></tr></table></dt>
<dd>
  Macro form of <tt class="cfunction">PyString_AsString()</tt> but without error
  checking.  Only string objects are supported; no Unicode objects
  should be passed.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int&nbsp;<b><tt id='l2h-462' xml:id='l2h-462' class="cfunction">PyString_AsStringAndSize</tt></b>(</nobr></td><td>PyObject *<var>obj</var>,
                                                 char **<var>buffer</var>,
                                                 int *<var>length</var>)</td></tr></table></dt>
<dd>
  Return a NUL-terminated representation of the contents of the
  object <var>obj</var> through the output variables <var>buffer</var> and
  <var>length</var>.

<P>
The function accepts both string and Unicode objects as input. For
  Unicode objects it returns the default encoded version of the
  object.  If <var>length</var> is <tt class="constant">NULL</tt>, the resulting buffer may not
  contain NUL characters; if it does, the function returns <code>-1</code>
  and a <tt class="exception">TypeError</tt> is raised.

<P>
The buffer refers to an internal string buffer of <var>obj</var>, not a
  copy. The data must not be modified in any way, unless the string
  was just created using <code>PyString_FromStringAndSize(NULL,
  <var>size</var>)</code>.  It must not be deallocated.  If <var>string</var> is a
  Unicode object, this function computes the default encoding of
  <var>string</var> and operates on that.  If <var>string</var> is not a string
  object at all, <tt class="cfunction">PyString_AsStringAndSize()</tt> returns 
  <code>-1</code> and raises <tt class="exception">TypeError</tt>.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-463' xml:id='l2h-463' class="cfunction">PyString_Concat</tt></b>(</nobr></td><td>PyObject **<var>string</var>,
                                         PyObject *<var>newpart</var>)</td></tr></table></dt>
<dd>
  Create a new string object in <var>*string</var> containing the contents
  of <var>newpart</var> appended to <var>string</var>; the caller will own the
  new reference.  The reference to the old value of <var>string</var> will
  be stolen.  If the new string cannot be created, the old reference
  to <var>string</var> will still be discarded and the value of
  <var>*string</var> will be set to <tt class="constant">NULL</tt>; the appropriate exception will
  be set.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-464' xml:id='l2h-464' class="cfunction">PyString_ConcatAndDel</tt></b>(</nobr></td><td>PyObject **<var>string</var>,
                                               PyObject *<var>newpart</var>)</td></tr></table></dt>
<dd>
  Create a new string object in <var>*string</var> containing the contents
  of <var>newpart</var> appended to <var>string</var>.  This version decrements
  the reference count of <var>newpart</var>.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>int&nbsp;<b><tt id='l2h-465' xml:id='l2h-465' class="cfunction">_PyString_Resize</tt></b>(</nobr></td><td>PyObject **<var>string</var>, int <var>newsize</var>)</td></tr></table></dt>
<dd>
  A way to resize a string object even though it is ``immutable''.
  Only use this to build up a brand new string object; don't use this
  if the string may already be known in other parts of the code.  It
  is an error to call this function if the refcount on the input string
  object is not one.
  Pass the address of an existing string object as an lvalue (it may
  be written into), and the new size desired.  On success, <var>*string</var>
  holds the resized string object and <code>0</code> is returned; the address in
  <var>*string</var> may differ from its input value.  If the
  reallocation fails, the original string object at <var>*string</var> is
  deallocated, <var>*string</var> is set to <tt class="constant">NULL</tt>, a memory exception is set,
  and <code>-1</code> is returned.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-466' xml:id='l2h-466' class="cfunction">PyString_Format</tt></b>(</nobr></td><td>PyObject *<var>format</var>,
                                              PyObject *<var>args</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Return a new string object from <var>format</var> and <var>args</var>.
  Analogous to <code><var>format</var> % <var>args</var></code>.  The <var>args</var>
  argument must be a tuple.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>void&nbsp;<b><tt id='l2h-467' xml:id='l2h-467' class="cfunction">PyString_InternInPlace</tt></b>(</nobr></td><td>PyObject **<var>string</var>)</td></tr></table></dt>
<dd>
  Intern the argument <var>*string</var> in place.  The argument must be
  the address of a pointer variable pointing to a Python string
  object.  If there is an existing interned string that is the same as
  <var>*string</var>, it sets <var>*string</var> to it (decrementing the
  reference count of the old string object and incrementing the
  reference count of the interned string object), otherwise it leaves
  <var>*string</var> alone and interns it (incrementing its reference
  count).  (Clarification: even though there is a lot of talk about
  reference counts, think of this function as reference-count-neutral;
  you own the object after the call if and only if you owned it before
  the call.)
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-468' xml:id='l2h-468' class="cfunction">PyString_InternFromString</tt></b>(</nobr></td><td>const char *<var>v</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  A combination of <tt class="cfunction">PyString_FromString()</tt> and
  <tt class="cfunction">PyString_InternInPlace()</tt>, returning either a new string
  object that has been interned, or a new (``owned'') reference to an
  earlier interned string object with the same value.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-469' xml:id='l2h-469' class="cfunction">PyString_Decode</tt></b>(</nobr></td><td>const char *<var>s</var>,
                                               int <var>size</var>,
                                               const char *<var>encoding</var>,
                                               const char *<var>errors</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Create an object by decoding <var>size</var> bytes of the encoded
  buffer <var>s</var> using the codec registered for
  <var>encoding</var>.  <var>encoding</var> and <var>errors</var> have the same
  meaning as the parameters of the same name in the
  <tt class="function">unicode()</tt> built-in function.  The codec to be used is
  looked up using the Python codec registry.  Return <tt class="constant">NULL</tt> if
  an exception was raised by the codec.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-470' xml:id='l2h-470' class="cfunction">PyString_AsDecodedObject</tt></b>(</nobr></td><td>PyObject *<var>str</var>,
                                               const char *<var>encoding</var>,
                                               const char *<var>errors</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Decode a string object by passing it to the codec registered for
  <var>encoding</var> and return the result as Python
  object. <var>encoding</var> and <var>errors</var> have the same meaning as the
  parameters of the same name in the string <tt class="method">encode()</tt> method.
  The codec to be used is looked up using the Python codec registry.
  Return <tt class="constant">NULL</tt> if an exception was raised by the codec.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-471' xml:id='l2h-471' class="cfunction">PyString_Encode</tt></b>(</nobr></td><td>const char *<var>s</var>,
                                               int <var>size</var>,
                                               const char *<var>encoding</var>,
                                               const char *<var>errors</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Encode the <tt class="ctype">char</tt> buffer of the given size by passing it to
  the codec registered for <var>encoding</var> and return a Python object.
  <var>encoding</var> and <var>errors</var> have the same meaning as the
  parameters of the same name in the string <tt class="method">encode()</tt> method.
  The codec to be used is looked up using the Python codec
  registry.  Return <tt class="constant">NULL</tt> if an exception was raised by the
  codec.
</dd></dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"><td><nobr>PyObject*&nbsp;<b><tt id='l2h-472' xml:id='l2h-472' class="cfunction">PyString_AsEncodedObject</tt></b>(</nobr></td><td>PyObject *<var>str</var>,
                                               const char *<var>encoding</var>,
                                               const char *<var>errors</var>)</td></tr></table></dt>
<dd>
<div class="refcount-info">
  <span class="label">Return value:</span>
  <span class="value">New reference.</span>
</div>
  Encode a string object using the codec registered for
  <var>encoding</var> and return the result as Python object.
  <var>encoding</var> and <var>errors</var> have the same meaning as the
  parameters of the same name in the string <tt class="method">encode()</tt> method.
  The codec to be used is looked up using the Python codec registry.
  Return <tt class="constant">NULL</tt> if an exception was raised by the codec.
</dd></dl>

<P>

<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.3 Sequence Objects"
  href="sequenceObjects.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="7.3 Sequence Objects"
  href="sequenceObjects.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.3.2 Unicode Objects"
  href="unicodeObjects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python/C API Reference Manual</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><img src='../icons/blank.png'
  border='0' height='32'  alt='' width='32' /></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="sequenceObjects.html">7.3 Sequence Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="sequenceObjects.html">7.3 Sequence Objects</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="unicodeObjects.html">7.3.2 Unicode Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.3, documentation updated on 29 March 2006.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>