How to make string check case insensitive?

前端 未结 4 648
难免孤独
难免孤独 2020-12-05 11:15

I\'ve started learning Python recently and as a practise I\'m working on a text-based adventure game. Right now the code is really ineffective as it checks the user responce

4条回答
  •  没有蜡笔的小新
    2020-12-05 11:36

    if 'power' in choice.lower():
    

    should do (assuming choice is a string). This will be true if choice contains the word power. If you want to check for equality, use == instead of in.

    Also, if you want to make sure that you match power only as a whole word (and not as a part of horsepower or powerhouse), then use regular expressions:

    import re
    if re.search(r'\bpower\b', choice, re.I):
    

提交回复
热议问题