<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>5.2.6 String conversions</title> <META NAME="description" CONTENT="5.2.6 String conversions"> <META NAME="keywords" CONTENT="ref"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <meta http-equiv="Content-Type" content="text/html; charset="> <link rel="STYLESHEET" href="ref.css"> <link rel="first" href="ref.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="previous" href="dict.html"> <LINK REL="up" href="atoms.html"> <LINK REL="next" href="primaries.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="dict.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="atoms.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="primaries.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Reference Manual</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="dict.html">5.2.5 Dictionary displays</A> <b class="navlabel">Up:</b> <a class="sectref" href="atoms.html">5.2 Atoms</A> <b class="navlabel">Next:</b> <a class="sectref" href="primaries.html">5.3 Primaries</A> <br><hr> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION007260000000000000000"> </A> <BR> 5.2.6 String conversions </H2> <a name="l2h-243"> </a> <P> A string conversion is an expression list enclosed in reverse (a.k.a. backward) quotes: <P> <dl><dd class="grammar"> <div class="productions"> <table cellpadding="2"> <tr valign="baseline"> <td><code><a name="tok-string_conversion">string_conversion</a></code></td> <td> ::= </td> <td><code>"`" <a href="exprlists.html#tok-expression_list">expression_list</a> "`"</code></td></tr> </table> </div> <a class="grammar-footer" href="grammar.txt" type="text/plain" >Download entire grammar as text.</a> </dd></dl> <P> A string conversion evaluates the contained expression list and converts the resulting object into a string according to rules specific to its type. <P> If the object is a string, a number, <code>None</code>, or a tuple, list or dictionary containing only objects whose type is one of these, the resulting string is a valid Python expression which can be passed to the built-in function <tt class="function">eval()</tt> to yield an expression with the same value (or an approximation, if floating point numbers are involved). <P> (In particular, converting a string adds quotes around it and converts ``funny'' characters to escape sequences that are safe to print.) <P> It is illegal to attempt to convert recursive objects (e.g., lists or dictionaries that contain a reference to themselves, directly or indirectly.) <a name="l2h-244"> </a> <P> The built-in function <tt class="function">repr()</tt> performs exactly the same conversion in its argument as enclosing it in parentheses and reverse quotes does. The built-in function <tt class="function">str()</tt> performs a similar but more user-friendly conversion. <a name="l2h-245"> </a> <P> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="dict.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="atoms.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="primaries.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Reference Manual</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="dict.html">5.2.5 Dictionary displays</A> <b class="navlabel">Up:</b> <a class="sectref" href="atoms.html">5.2 Atoms</A> <b class="navlabel">Next:</b> <a class="sectref" href="primaries.html">5.3 Primaries</A> <hr> <span class="release-info">Release 2.2, documentation updated on December 21, 2001.</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>