【问题标题】:Why does len(str(int)) = 13?为什么 len(str(int)) = 13?
【发布时间】:2018-11-23 04:45:47
【问题描述】:

我正在做一些 python 并注意到:

print(len(str(int))

产量为 13。 需要明确的是, int 只是整数类,没有分配给它的变量。你可以只运行这段代码,它会产生 13。 现在我想起来了,它可以同时打印“字符串”和“整数”的长度吗?如果有,为什么?

【问题讨论】:

  • 字符串"<class 'int'>"的长度为13个字符
  • 因为类也有 __str____repr__ 表示,对于 int 它是 <type 'int'>
  • 您是否先尝试打印str(int)

标签: python string class string-length


【解决方案1】:

str(int) 返回<class 'int'>,长度为 13 个字符。

【讨论】:

    【解决方案2】:

    在你的解释器上输入int:

    >>> int
    <class 'int'>
    >>> 
    

    结果将是&lt;class 'int'&gt;

    所以将其转换为字符串将是"&lt;class 'int'&gt;"的字符串,其中包含13个字符。

    完整的例子:

    >>> int
    <class 'int'>
    >>> str(int)
    "<class 'int'>"
    >>> len(str(int))
    13
    >>> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2018-01-04
      • 2018-12-29
      • 2019-12-01
      • 2011-05-15
      • 2020-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多