• 技术文章 >常见问题 >Python常见问题

    python怎样判断字符串相等

    silencementsilencement2020-03-18 15:23:41原创3255

    python字符串如何判断相等

    1.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-SummaryListHandler', False)
                if groupName is 'url':
    
    打印如下,看到通过is判断是false
    ('reqBody_dim-SummaryListHandler', u'url')
    ('reqBody_dim_url-SummaryListHandler', False)

    2.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怎么自动处理excel 下一篇:python怎么使用迭代器

    相关文章推荐

    • python中数组怎么转换为字符串• python中字符串怎么转换成int• python字符串乱码怎么办• python如何把字符串切片

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网