1

I am trying to check the equality of two strings however it seems that my code it doesnt work properly:

listes = []
for row in my_lines:
    split = re.split(r' +', row)
    print split[0], ":size of the split: ", len(split)
    if str(split[0]) == '5':
        print "...."

The printed message from my print before the if statement is the following:

'5' :size of the split:  3
'4' :size of the split:  4
'6' :size of the split:  3
'6' :size of the split:  4
'F' :size of the split:  4
'6' :size of the split:  4
'F' :size of the split:  4
'6' :size of the split:  4

However the if statement does not work. What could have been wrong here?

3
  • 3
    It appears that your split[x] contains a string of the form "'x'", but you're only comparing it to "x". Commented Dec 20, 2016 at 11:19
  • 3
    Looks like the string is actually '5' with single quotation marks and space. Commented Dec 20, 2016 at 11:19
  • Yes you are right. Thanks for the answers! Commented Dec 20, 2016 at 11:20

1 Answer 1

4

That is because your split[0] content itself has ' as the part of the string based on the output you mentioned. You need to either compare it like:

  if str(split[0]) == "'5'":  
  #                    ^ ^ single quotes here

OR, remove ' from start and end of row as:

 if str(split[0])[1:-1] == "5":
 #                ^  ^ remove first and last character from string
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.