Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较python中的两个字符串_Python_String - Fatal编程技术网

比较python中的两个字符串

比较python中的两个字符串,python,string,Python,String,python中是否有任何内置函数可以启用两个比较字符串。 我尝试使用==运算符比较两个字符串,但不起作用 try: if company=="GfSE-Zertifizierungen": y=2 if x<y: print "**************Same company***********" x=x+1 flag=0 pass

python中是否有任何内置函数可以启用两个比较字符串。 我尝试使用
==
运算符比较两个字符串,但不起作用

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
试试看:
如果公司==“GfSE Zertifizierungen”:
y=2

如果python中的x用于比较字符串,则应使用
=
运算符。 例如:

然后


建议:打印变量“company”的内容,检查其中的内容。请确保大小写相同(小写/大写字母)。

您可以使用
=
检查两个字符串是否相等。 问题不在于您的
if
语句

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

OK

您可以使用
调试器
查看代码有什么问题。

python中字符串的
==
运算符将一个字符串的每个字母与另一个字符串进行比较。如果它们都相同,则字符串相等

这里唯一的两种可能性是你没有到达终点线

if company=="GfSE-Zertifizierungen":
或者公司实际上是不一样的

要帮助进行故障排除,请添加以下内容:

try:
    print "Got to here"
    print company
    if company=="GfSE-Zertifizierungen":
        y=2
        ....

如果公司==“GfSE Zertifizierungen”:
则在
后面放一条打印语句。看看能不能印出来。如果是,您知道错误不在If-test中。
x
y
与比较字符串有什么关系?请检查您的格式;缩进在Python中很重要。什么是
公司
?请注意,例如,
“GfSE Zertifizierungen”!=“GfSE Zertifizierungen”
。此外,在
try
块中,您应该尽可能少地输入代码;将其余部分移到
else
。此处发布的代码混合了制表符和空格。要小心,最好只使用空格。否则,最终会出现不一致的缩进,这只会导致错误。
if company=="GfSE-Zertifizierungen":
try:
    print "Got to here"
    print company
    if company=="GfSE-Zertifizierungen":
        y=2
        ....