如何在python中比较字符串?

如何在python中比较字符串?,python,string,compare,string-comparison,Python,String,Compare,String Comparison,我需要在一个元素数组中存储一个字符串,当我存储和比较它们时,它们是不同的。我不确定我错过了什么 test = np.zeros(1,dtype=[('data','a8')]) test['data']=str("right") print(test[0]['data'], test[0]['data'] == "right") 我的输出是b'right'False。但是,它应该是True。您正在比较字符串和字节(注意打印输出中的b)。试一试 b'right'!='右“;您需要使用适

我需要在一个元素数组中存储一个字符串,当我存储和比较它们时,它们是不同的。我不确定我错过了什么

test = np.zeros(1,dtype=[('data','a8')]) 

test['data']=str("right") 
print(test[0]['data'], test[0]['data'] == "right")  

我的输出是
b'right'False
。但是,它应该是
True

您正在比较字符串和字节(注意打印输出中的b)。试一试


b'right'!='右“
;您需要使用适当的编码对字节进行解码,以与字符串进行比较。例如,
b'right'。解码('utf8')=='right'
。没问题。如果你满意的话,你应该接受这个答案。
print(test[0]['data'] == b"right")