[英]How to convert string number to int and avoid the string text before convert
我想创建检查条件字符串是否为字符串编号,我该怎么做 python
x = "String"
y = "12"
if x is string:
print("this is string")
if y is string:
print("this is not string")
你可以尝试这样的事情:
array = ["String", "12"]
for item in array:
try:
int(item)
print(f"{item} is not string")
except:
print(f"{item} is string")
您可以使用.isdigit()
方法来实现这一点。
x = "String"
y = "12"
if not(x.isdigit()):
print("this is string")
if y.isdigit():
print("this is not string")
Output:
this is string
this is not string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.