擅长:python、mysql、java
<p>Python中的字符串连接只在字符串之间起作用。它不像其他语言那样根据需要推断类型。在</p>
<p>有两个选项,将整数转换为字符串并将其全部相加:</p>
<pre><code>>>> x ="a"
>>> y = 1
>>> "(" + x + "," + str(y) + ")"
'(a,1)'
>>> "('" + x + "'," + str(y) + ")"
"('a',1)"
>>> "(" + repr(x) + "," + str(y) + ")"
"('a',1)"
</code></pre>
<p>或者使用字符串格式在幕后处理这些问题。使用<a href="http://docs.python.org/2.7/library/stdtypes.html#string-formatting" rel="nofollow noreferrer">(deprecated) "percent formatting"</a>:</p>
^{pr2}$
<p>或者更多<a href="http://docs.python.org/2.7/library/string.html#format-specification-mini-language" rel="nofollow noreferrer">standard and approved format mini-language</a>:</p>
<pre><code>>>> "({0},{1})".format(x, y)
'(a,1)'
>>> "('{0}',{1})".format(x, y)
"('a',1)"
>>> "({0},{1})".format(repr(x), y)
"('a',1)"
</code></pre>