簡體   English   中英

如何將字符串數字轉換為int並避免轉換前的字符串文本

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM