1. Oben Sonne
  2. obensonne.bitbucket.org

Source

obensonne.bitbucket.org / blog / 20110127-python-not-all-strings.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="description" content="Obvious and hidden reasons for the regular Python error." />
    <meta name="keywords" content="python, error, string" />
    <link rel="alternate" type="application/rss+xml" href="http://obensonne.bitbucket.org/rss.xml" title="Oben Sonne" />
    <title>Oben Sonne - blog</title>
    <link href="http://obensonne.bitbucket.org/style/reset.css" rel="stylesheet" type="text/css" />
    <link href="http://obensonne.bitbucket.org/style/960.css" rel="stylesheet" type="text/css" />
    <link href="http://obensonne.bitbucket.org/style/text.css" rel="stylesheet" type="text/css" />
    <link href="http://obensonne.bitbucket.org/style/os.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
    /* <![CDATA[ */
        (function() {
            var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
            s.type = 'text/javascript';
            s.async = true;
            s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
            t.parentNode.insertBefore(s, t);
        })();
    /* ]]> */
    </script>
</head>
<body>
<div id="wrap" class="container_12">
  <div id="header" class="prefix_2 grid_3">
    <h1><a class="plain" href="http://obensonne.bitbucket.org/index.html">Oben Sonne</a></h1>
  </div>
  <div id="menu" class="grid_5 suffix_2">
<ul class="">
<li class=""><a href="http://obensonne.bitbucket.org/index.html">home</a></li>
<li class="current"><a href="http://obensonne.bitbucket.org/blog.html">blog</a></li>
<li class=""><a href="http://obensonne.bitbucket.org/projects.html">projects</a></li>
<li class=""><a href="http://obensonne.bitbucket.org/about.html">about</a></li>
</ul>
  </div>
  <div class="clear"> </div>
  <div id="content" class="prefix_2 grid_8 suffix_2">
    <div class="wrap">
      <h1 class="post-title">TypeError: not all arguments converted during string formatting</h1>

<p class="post-date">Posted on January 27, 2011</p>

<p>If you're programming Pyhon, you've probably seen this before:</p>
<pre><code>Traceback (most recent call last):
  File ...
TypeError: not all arguments converted during string formatting
</code></pre>
<p>The reason probably is that you passed more strings after the <code>%</code> character
than there are corresponding placeholders:</p>
<pre><code>&gt;&gt;&gt; values = ["foo", "bar]
&gt;&gt;&gt; print "values: s" % values
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: not all arguments converted during string formatting
</code></pre>
<p>The problem and its fix is simple -- add the missing <code>%</code>:</p>
<pre><code>&gt;&gt;&gt; print "values: %s" % values
values: ['foo', 'bar']
</code></pre>
<p>Not a big deal. However, recently I came across this:</p>
<pre><code>&gt;&gt;&gt; print "values: %s" % values
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: not all arguments converted during string formatting
</code></pre>
<p>What? Yes, that may happen if <code>values</code> is a tuple, in which case you need as
much format characters in your string as there are elements in the tuple:</p>
<pre><code>&gt;&gt;&gt; values = ("foo", "bar)
&gt;&gt;&gt; print "values: %s, %s" % values
values: foo, bar
</code></pre>
<p>Of course this is a bad solution as it you have to know the tuple size when
coding. Addtionally it does not really <em>dump</em> <code>value</code>, what </p>
    </div>
  </div>
  <div id="footer" class="prefix_2 grid_8 suffix_2">
    A site by <a href="http://obensonne.bitbucket.org/">Oben Sonne</a>
    &middot;
    Licensed as <a href="http://creativecommons.org/licenses/by-sa/3.0">CC-by-SA</a>
    &middot;
    Built with <a href="http://bitbucket.org/obensonne/poole">Poole</a>
    &middot;
    <a href="http://validator.w3.org/check?uri=referer">Validate me</a>
  </div>
  <div class="clear"></div>
</div>

<!-- analytics -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10517530-5");
pageTracker._trackPageview();
} catch(err) {}</script>
<!-- end: analytics -->
<!-- plusone -->
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<!-- end: plusone -->
<!-- disqus -->
<script type="text/javascript">
//<![CDATA[
(function() {
    var links = document.getElementsByTagName('a');
    var query = '?';
    for(var i = 0; i < links.length; i++) {
    if(links[i].href.indexOf('#disqus_thread') >= 0) {
        query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
    }
    }
    document.write('<script charset="utf-8" type="text/javascript" src="http://disqus.com/forums/obensonne/get_num_replies.js' + query + '"></' + 'script>');
})();
//]]>
</script>
<!-- end: disqus -->
</body>
</html>