0

I am writing a code where I am facing the problem and need a solution if it exists. Suppose we have a following String type variable in Python which contains an integer value. Eg:x='123' I know that we can easily convert this by type conversion to int. However, suppose we have the following list.

x=['123','Spain']

Is there any method in Python by which I can know which element of the list x is Integer contained inside a string and which is purely an Object?

Soothy
  • 34
  • 7

2 Answers2

0

I would recommend this method:

x = "123"
if x.isdigit():
    # int
elif x.replace(".","",1).isdigit():
    # float
else:
    # str

CMinusMinus
  • 426
  • 1
  • 3
  • 11
0

I assume you have similar question with this post.

But, from my perspective, for more general solution (language agnostic), you should learn more about Regular Expression, here also the same question