-2

I need this little snippet to output "this is a string" (i need myVar to satisfy the condition is str)

myVar = 'some-string'
if myVar is str:
    print('this is a string')
else:
    print('this is NOT a string')

but when i run it, it keeps outputting "this is NOT a string". I don't understand, can someone please point out what I'm doing wrong and how to fix it?

I've also tried:

myVar = str('some-string')
if myVar is str:
    print('this is a string')
else:
    print('this is NOT a string')

and it also doesn't work.

I can't use isinstance() to check anything, I MUST keep the conditional of

if myVar is str:

and this conditional must evaluate to True.

i've also tried this:

if 'some-string' is str:
    print('this is a string')
else:
    print('this is NOT a string')

and this also outputs "this is NOT a string"

I don't know what i need to do to feed it something which satisfies this condition

Qntn Yvrny
  • 27
  • 4
  • See [this question](https://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python) to understand what `is` does. – Aran-Fey Sep 09 '18 at 09:48
  • 1
    *"I can't use isinstance() to check anything, I MUST keep the conditional"* That's unfortunate. You'll have to rewrite the python interpreter if you want that to work. Good luck. – Aran-Fey Sep 09 '18 at 09:49
  • Please mark one of the solutions as correct – CodeSamurai777 Sep 09 '18 at 09:55

4 Answers4

2

Two ways:

if type(myVar) is str:

and

if isinstance(myVar, str):
Austin
  • 25,759
  • 4
  • 25
  • 48
0

Your check is incorrect, str is an object of class type which can create instances of class str the correct check is:

myVar = str('some-string')
if isinstance(myVar,str):
    print('this is a string')
else:
    print('this is NOT a string')

The same way given class A this check:

if x is A is incorrect

Defining a class creates a special object of class type that is responsible for creating instances of the defined class that is why you need to use isinstace() or issubclass() which uses this special object and the object given to answer the query. Because simple is checks if object x is object A and that is not true.

CodeSamurai777
  • 3,285
  • 2
  • 24
  • 42
  • when i try your code, it does indeed output 'this is a string', thank you! I will try to correct my situation – Qntn Yvrny Sep 09 '18 at 09:54
0

If you want to check if an object is a string (or any type for that matter) use isinstance(Myvar, str) which will be True if Myvar is string.

So:

myvar = 'this is a string'
if isinstance(myvar, str):
    print(f'the variable {myvar} is a string')
else:
    print(f'the variable {myvar} is not a string')

To be pythonic do not use capitals for variables...

Bruno Vermeulen
  • 2,970
  • 2
  • 15
  • 29
0
>>> string = "Hello"
>>> isinstance(string,str)
True
>>> isinstance(1,str)
False
>>> if isinstance(string,str):
    print("this is a string")
else:
    print("this is not a string")

this is a string
>>> 
Nouman
  • 6,947
  • 7
  • 32
  • 60