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

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.
    – Lee White
    Commented Apr 8, 2014 at 12:40
  • 1
    What do x and y have to do with comparing strings?
    – poke
    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.
    – jonrsharpe
    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).

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
        ....
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.