我不认为有一个一行程序或内置函数用于特定目的,但一个简单的if语句应该可以工作。
strX = "-5" strX = int(strX) if strX >= 0 or strX >= -0: print (strX) else: print ("Not a non-negative value.")
您的例子是用Python 2.7语法编写的,但由于我不太满意,所以我在Python 3中重写了它。X. Sorry给您带来了不便。