-1

How can I make an if statement that will ask if my variable contains string or any type then if it is, then it will execute the code under my if statement. Below code is just my experimentation but it does not work the way I hope it'll be.

def checkMyValues(*args):
    if isinstance(args, str) == True:
        print("it is a string!")

checkMyValues("haime")

but this does not output "it is a string!".

any help will be much appreciated.Thanks

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
Joseph Reyes
  • 69
  • 1
  • 3
  • 10

6 Answers6

1

When you want to check the list of arguments for the type, you should loop it rather than checking the tuple itself for its type. then it will give you the expected results. Below is the modified version of your code

def checkMyValues(*args):
    for each in args:        
        if isinstance(each, str) == True:
            print("it is a string!")
        else:
            print("Its not a string")

checkMyValues("haime", 20, '40.2')
NMN
  • 372
  • 3
  • 16
  • Figured it out after a couple of minutes I ask this question. I got same solution as your answer thanks! Can remove the " == True" by the way and works just fine. – Joseph Reyes Apr 25 '17 at 07:43
  • Cool. Yes, the isinstance() function returns a boolean value, it works fine without '=='. – NMN Apr 25 '17 at 07:48
0

Using *args in your function will make args to be a tuple and not str that is why it won't print it is a string.

Try this

def checkMyValues(arg):
    if isinstance(arg, str): # Not need to compare == True
        print("it is a string!")

checkMyValues("haime")

More info on *args and *kwargs here

VMRuiz
  • 1,931
  • 12
  • 20
  • Yea but I want it as *args because i'm working on a flexible number of arguments. I think I got it. I need to loop it first in a For loop and get each value of args before doing anything on it. Thanks btw – Joseph Reyes Apr 25 '17 at 07:31
  • `*args` makes a tuple, not a list. Can be an important distinction. – Ted Klein Bergman Apr 25 '17 at 07:32
0

Remove * from args and it will work. Adding * to arguments makes it a non-keyworded argument (List). Thus your check fails.

Vaibhav Singh
  • 932
  • 7
  • 20
0

you need to loop over the args (which is a tuple of the arguments you are passing to the function):

def checkMyValues(*args):
    for arg in args:
        if isinstance(arg, str):
            print(arg, "is a string!")

which outputs:

checkMyValues("haime")
# haime is a string!

checkMyValues("haime", 7, [], None, 'strg')
# haime is a string!
# strg is a string!
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
-1

*args is a tuple not a string

def checkMyValues(*args): 
    for s in args:  
        z = type(s)
        if  z is str:
            print(s," is a string!")
        else:
            print(s," is not a string!")

checkMyValues("4","5",5)
Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
-1

I don't quite understand what do you mean, but according to your code. Maybe you need this.

def checkMyValues(args):
    if isinstance(args, str):
        print("is string")
Han Xipeng
  • 46
  • 5