我需要一个由重复特定字符组成的字符串.在Python控制台,如果我输入:
n = '0'*8
Run Code Online (Sandbox Code Playgroud)
然后n被赋予一个由8个零组成的字符串,这是我所期望的.
但是,如果我在Python程序(.py文件)中使用相同的程序,那么程序会因错误而中止
can't multiply sequence by non-int of type 'str'
有任何解决这个问题的方法吗 ?
小智 33
你得到那个错误,因为 - 在你的程序中 - 8实际上也是一个字符串.
>>> '0'*8
'00000000'
>>> '0'*'8' # note the ' around 8
(I spare you the traceback)
TypeError: can't multiply sequence by non-int of type 'str'
Run Code Online (Sandbox Code Playgroud)