简体   繁体   English

如何将字符串数字转换为int并避免转换前的字符串文本

[英]How to convert string number to int and avoid the string text before convert

I would like to create the check condition string is string number or not so, how I can do in python我想创建检查条件字符串是否为字符串编号,我该怎么做 python

x = "String"
y = "12"

if x is string:
   print("this is string")
if y is string:
   print("this is not string")

You could try something like this:你可以尝试这样的事情:

array = ["String", "12"]

for item in array:
  try:
    int(item)
    print(f"{item} is not string")
  except:
    print(f"{item} is string")

You can use .isdigit() method to fulfill this.您可以使用.isdigit()方法来实现这一点。

x = "String"
y = "12"

if not(x.isdigit()):
   print("this is string")
if y.isdigit():
   print("this is not string")

Output: Output:

this is string
this is not string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM