26

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 to see if it is the same as several variations on the same word. How do I change it so the string check is case insensitive?

Example code below:

if str('power' and 'POWER' and 'Power') in str(choice):
    print('That can certainly be found here.')
    time.sleep(2)
    print('If you know where to look... \n')
1
  • 3
    Please note that the expression 'power' and 'POWER' doesn't do what you think it does, it merely evaluates to 'POWER', and that calling str(...) on the strings is probably redundant and possibly breaks your program (e.g. it could break it for some custom string or string-like classes). Commented May 4, 2011 at 21:20

5 Answers 5

39
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):
6
  • See below, double commented. :S
    – Jack
    Commented May 4, 2011 at 21:44
  • Thanks a LOT for that. Question, though: How does choice.lower() work? I assumed this means it would seach the lowercase words of choice only.
    – Jack
    Commented May 4, 2011 at 21:44
  • The .lower() method returns the lowercase equivalent of the string it is applied to. Open your Python interpreter and enter "ErklÄrBäR".lower(). You'll get 'erklärbär'. Commented May 5, 2011 at 6:25
  • And how does searching the lower case equivelant of a lower case word make it case insensitive, exactly? xD
    – Jack
    Commented May 5, 2011 at 19:05
  • "power" == "POWER".lower() == "PoWeR".lower() == "power".lower() Commented May 5, 2011 at 19:55
11

This if you're doing exact comparison.

if choice.lower() == "power":

Or this, if you're doing substring comparison.

if "power" in choice.lower():

You also have choice.lower().startswith( "power" ) if that interests you.

5

The str type/object has a method specifically intended for caseless comparison.

At the python3 prompt:

>>> help(str)
...
 |  casefold(...)
 |      S.casefold() -> str
 |      
 |      Return a version of S suitable for caseless comparisons.
...

So, if you add .casefold() to the end of any string, it will give you all lowercase.

Examples:

>>> "Spam".casefold()
'spam'
>>> s = "EggS"
>>> s.casefold()
'eggs'
>>> s == "eggs"
False
>>> s.casefold() == "eggs"
True
4

use str.lower() to convert all entries to lowercase and only check the string against lower case possibilities.

3

Use .casefold() over .lower(), especially when working with data that might not be ASCII text.

>>> 'MyString'.casefold()
'mystring'

See https://stackoverflow.com/a/45745761/14816491 for detailed information on the differences between casefold and lower

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.