2

Is there any in build function in python which enables two compare two string. i tried comparing two strings using == operator but not working.

try:
 if company=="GfSE-Zertifizierungen":
 y=2
 if x<y:
 print "**************Same company***********"
 x=x+1
 flag=0
 pass 
 if x==y:
 flag=1
 x=0
 count=count+1
except Exception as e:
 print e

This is not even showing any error nor working out. Can anyone assist me where I m going wrong

Xantium
11.7k12 gold badges72 silver badges95 bronze badges
asked Apr 8, 2014 at 12:38
4
  • Put a print statement right after if company=="GfSE-Zertifizierungen":. See if that gets printed. If it does, you know that the error doesn't lie in that if-test. Commented Apr 8, 2014 at 12:40
  • 1
    What do x and y have to do with comparing strings? Commented Apr 8, 2014 at 12:40
  • 1
    Please review your formatting; indentation is important in Python. What is company? Note that e.g. "GfSE-Zertifizierungen" != "GfSE Zertifizierungen". Also, you should have as little as possible in the try block; move the rest to an else. Commented Apr 8, 2014 at 12:41
  • 1
    Your code, as posted here is mixing tabs and spaces. Be careful, best to stick to just spaces. Otherwise you'll end up with inconsistent indentation and that'll only lead to errors. Commented Apr 8, 2014 at 12:46

3 Answers 3

2

In python to compare a string you should use the == operator. eg:

a = "hello"
b = "hello2"
c = "hello"

then

a == b # should return False
a == c # should return True

Suggestion: print the content of your variable "company" to check what's inside of it. Be sure to have the same case (lower/upper letters).

answered Apr 8, 2014 at 12:43

Comments

0

The == operator for strings in python compares each letter of one string to another. If they are all the same, the string is equal.

The only two possibilities here are that you are not reaching the line

if company=="GfSE-Zertifizierungen":

or company is not actually the same.

To help troubleshoot, add something like:

try:
 print "Got to here"
 print company
 if company=="GfSE-Zertifizierungen":
 y=2
 ....
answered Apr 8, 2014 at 12:46

Comments

0

You can use == to check both the string are equal or not. The problem is not with your if statement.

>>> company="GfSE-Zertifizierungen"
>>> if company == "GfSE-Zertifizierungen":
 print "OK"
 else:
 print "NOT OK"

Output:

OK

You can use debugger to see whats wrong with your code.

answered Apr 8, 2014 at 12:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.