擅长:python、mysql、java
<p>如果要计算位数,则需要将其转换回字符串(注意减号(<code>-</code>)也将计算为一):</p>
<pre><code>len(str(TestV))
</code></pre>
<p>或者只是等待将其转换为int:</p>
<pre><code>TestV = input("Data: ")
print len (TestV)
TestV = int(TestV)
</code></pre>
<p>注意,与后一种情况相比,它还将计算尾随空间等。</p>
<p><strong>编辑</strong>:根据您的评论。</p>
<p>原因是包含其他字符的字符串可能不是数字,而是过程中的错误。如果您想过滤掉这个数字,可以使用regex:</p>
<pre><code>import re
x = input("Data: ")
x = re.sub("\D", "", x)
xv = int(x)
</code></pre>