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
3 Answers 3
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).
Comments
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
....
Comments
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.
if company=="GfSE-Zertifizierungen":
. See if that gets printed. If it does, you know that the error doesn't lie in that if-test.x
andy
have to do with comparing strings?company
? Note that e.g."GfSE-Zertifizierungen" != "GfSE Zertifizierungen"
. Also, you should have as little as possible in thetry
block; move the rest to anelse
.