我已经搜索了几个答案,它们都不起作用。删除整数的最后一个数
下面是一个例子,但代码不起作用,并给出错误。我正在使用Python 2.7。
operationTwo = 91239
operationTwo = operationTwo[:-1]
print(operationTwo)
我已经搜索了几个答案,它们都不起作用。删除整数的最后一个数
下面是一个例子,但代码不起作用,并给出错误。我正在使用Python 2.7。
operationTwo = 91239
operationTwo = operationTwo[:-1]
print(operationTwo)
您发现该代码是slicing
,工程,但不是在integers
。如果你想使用它,你可以将数字转换为str
进行切片,然后将其转换回int
。这是不是最好的做法,但它是可以做到以下几点:
operationTwo = 91239
operationTwo = int(str(operationTwo)[:-1])
print(operationTwo)
我会然而,随着integer
师去,如:
operationTwo = 91239
operationTwo = operationTwo // 10
print(operationTwo)
这两种方法,如写入,将失败,如果整数是一个数字。在字符串切片中不要尝试int()它。在这两种情况下,最好测试一个数字,因为正确答案是空格(无)而不是零。 –