-1

I have two strings that I compare, but I am not getting the result I want. Here's how I do it, with Python 2.7:

str1 = '0000644'
str2 = '0000644'

if str1 == str2:
    print 'true!'
else:
    print 'false'

I have also tried with the is comparison:

if str1 is str2:
    print 'true'
else:
    print 'false'

Can someone explain why I am not printing true when I do this? I come from C#, and if you do it like this you should print the true value.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
Code_Viking
  • 618
  • 1
  • 8
  • 26
  • Python requires indentation to separate blocks of code e.g. in case of `if` and `else`! Also, it should be `True` and `False` and a colon should come after `else` – kiner_shah Apr 30 '17 at 16:07
  • 1
    Also you're missing a colon and some quote marks (unless you meant boolean `True` and `False`, in which case you're missing the capital letters). `'0000644' == '0000644'` works as expected for me. – jonrsharpe Apr 30 '17 at 16:08
  • Include the actual exact code you are running. – Ned Batchelder Apr 30 '17 at 16:09
  • this is how the code look in the file – Code_Viking Apr 30 '17 at 16:09
  • The code you've now posted works just fine. Give a [mcve] that explains what problem, if any, you have. You say *"i am not printing true"*, so what happens instead? False? An error? Something else? – jonrsharpe Apr 30 '17 at 16:10
  • i just jumps to the else statement, it never hits the true, but i should hit the true, since the two str is the same? – Code_Viking Apr 30 '17 at 16:11
  • @KevinJensen you need to compare strings with `==`. What happens in your first example that you don't expect? – blubberdiblub Apr 30 '17 at 16:12
  • *"i just jumps to the else statement"* - ...according to what? Please [edit] the question to give some useful information; as it stands, it seems you're wrong about what it does. – jonrsharpe Apr 30 '17 at 16:13
  • 1
    Possible duplicate of [Is there a difference between \`==\` and \`is\` in Python?](http://stackoverflow.com/questions/132988/is-there-a-difference-between-and-is-in-python) – Pedro Lobito Apr 30 '17 at 16:13
  • @blubberdiblub in the first example i jumps to the else statement, and print false, i dont understand it? can i have somthing to do with the os i am using? – Code_Viking Apr 30 '17 at 16:13
  • 1
    @KevinJensen that would strike me as really strange. Check that the contents of the strings are really the same (no "O" instead of zero, no Unicode lookalikes, no typos) and that you're really using `==` to compare. – blubberdiblub Apr 30 '17 at 16:17

3 Answers3

4

The code you posted is not valid Python.

This will do:

str1 = '0000644'
str2 = '0000644'

if str1 == str2:
  print True
else:
  print False

To elaborate:

  • booleans start with capital letters: True and False (not sure why you had the exclamation)
  • blocks need to be consistently indented (unlike C# where you separate them with {})
  • else needs to finish with a colon

edit: my answer was based on OPs original code, which was not valid Python. I can't help if someone then changes the code into valid code after.

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
  • 2
    I don't think point 1 is really relevant. I was under the impression he just intended to print "true!", not necessarily the boolean literal. – Carcigenicate Apr 30 '17 at 16:13
  • 1
    In that case he should put it in double or single quotes. Hard to tell from his question so I made an assumption. – Jan Hančič Apr 30 '17 at 16:14
  • I wasn't the downvoter btw. That would be petty lol. And fair enough. – Carcigenicate Apr 30 '17 at 16:14
  • No worries, I haven't been on here for a while now. Is this what it has became? Downvotes for posting a correct answer? – Jan Hančič Apr 30 '17 at 16:15
  • 1
    The code you've posted is correct, but how does it solve the OP's problem? The code in the question as it stands is just like this, and still they claim it doesn't work. – jonrsharpe Apr 30 '17 at 16:16
  • @JanHančič People have been *very* brutal lately. I had a question, and saw another valid question that were downvoted within 30 seconds of posting, with no comment. I think downvotes are fine, but comment if you do! It's useless to downvote something where the reason for the downvote isn't obvious. I find it to be a dick move. I'm also bitter because it's happened to me on several occasions in the last week. – Carcigenicate Apr 30 '17 at 16:17
  • Maybe if people didn't edit OPs question and change the code into something that it isn't we wouldn't have this issue ... – Jan Hančič Apr 30 '17 at 16:17
  • @JanHančič ...the OP is the only person who's changed the code, as the history shows. – jonrsharpe Apr 30 '17 at 16:19
  • I see that now yeah, point still stands. I answered the original question, I can't monitor the question on the off chance the OP posted the wrong code and then edit my answer accordingly. I think I'm gonna go back to not contributing again, this is a bit silly. – Jan Hančič Apr 30 '17 at 16:21
0

is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.

>>> a = [17,27,37]
>>> b = a
>>> b is a 
True
>>> b == a
True
>>> b = a[:] #shallow copy of a
>>> b is a
False
>>> b == a
True
emegona
  • 168
  • 1
  • 12
-2

In Python, 'true' and 'false' booleans need to be capitalized at the 'T' and 'F', respectively. Also, when printing, whatever you want to print needs to be surrounded in double or single quotes.

K.Land_bioinfo
  • 170
  • 1
  • 3
  • 12