您现在的位置是:课程教程文章
python怎么比较字符串是否相等
2023-12-18 22:54课程教程文章 人已围观
在实际的开发工作中,经常会遇到要判定两个字符串是否相等,下面我们说一下具体的实现方法
第一种是is来判断
groupName = params['groupName'] ##groupName的值是'url' reqBody['dim'] = groupName print("reqBody_dim-SummaryListHandler", reqBody['dim']) ## ('reqBody_dim-SummaryListHandler', u'url') print("reqBody_dim_url-SummaryListHandler", ('url' is reqBody['dim'])) ## ('reqBody_dim_url-SummaryList Handler', False) if groupName is 'url': 打印如下,看到通过is判断是false ('reqBody_dim-SummaryListHandler', u'url') ('reqBody_dim_url-SummaryListHandler', False)
第二种是用id 和 in
看到用in可以判断两个字符串相等
print("groupName:",groupName) reqBody['dim'] = groupName print("reqBody_dim-SummaryListHandler", reqBody['dim']) print("reqBody_dim_url_is-SummaryListHandler", ('url' is groupName)) print("reqBody_dim_url_id-SummaryListHandler", (id('url')==id(groupName))) print("reqBody_dim_url_in-SummaryListHandler", ('url' in groupName)) ('groupName:', u'url') ('reqBody_dim-SummaryListHandler', u'url') ('reqBody_dim_url_is-SummaryListHandler', False) ('reqBody_dim_url_id-SummaryListHandler', False) ('reqBody_dim_url_in-SummaryListHandler', True)课程教程:python怎么比较字符串是否相等
上一篇:如何停止python程序
下一篇:没有了