-1

I would like to create the check condition string is string number or not so, how I can do in python

x = "String"
y = "12"

if x is string:
   print("this is string")
if y is string:
   print("this is not string")
  • What is `string` that you set in if condition? – mhhabib Dec 24 '20 at 02:03
  • `if x is string` If you want to check the _type_ of x, this is the wrong way to do it. Use `if type(x) is str` instead. – John Gordon Dec 24 '20 at 02:04
  • 2
    Does this answer your question? [How to check if type of a variable is string?](https://stackoverflow.com/questions/4843173/how-to-check-if-type-of-a-variable-is-string) – Montgomery Watts Dec 24 '20 at 02:05
  • 1
    Does this answer your question? [How can I check if a string represents an int, without using try/except?](https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except) – DarrylG Dec 24 '20 at 02:05
  • Both `x` and `y` are Python strings. – AMC Dec 24 '20 at 02:27

2 Answers2

0

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")
Graygood
  • 363
  • 1
  • 3
  • 18
0

You can use .isdigit() method to fulfill this.

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
Harshana
  • 5,151
  • 1
  • 17
  • 27