LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Case-Sensitivity in Python (https://www.linuxquestions.org/questions/programming-9/case-sensitivity-in-python-87122/)

Odd_Bloke 08-29-2003 06:28 PM

Case-Sensitivity in Python
 
How can I make Python recognise a string regardless of the case of it's contents? eg:

blah = [foo]
diddle = raw_input()
if diddle in blah:
happy_dance()

How can I get happy_dance() to run regardless of whether foo, Foo, FOo, FOO etc. is typed?

Cheers,
Dan

nouse66 08-30-2003 03:58 AM

Re: Case-Sensitivity in Python
 
Quote:

Originally posted by Odd_Bloke
How can I make Python recognise a string regardless of the case of it's contents? eg:

blah = [foo]
diddle = raw_input()
if diddle in blah:
happy_dance()

How can I get happy_dance() to run regardless of whether foo, Foo, FOo, FOO etc. is typed?

Cheers,
Dan

you can do something like this:

diddle = raw_input()
if diddle in ["foo", "Foo", "FOo", "FOO"]:
happy_dance()

Strike 09-02-2003 04:20 PM

Just store all uppercase versions and then compare the upper()'ed version of the entered string to the things in the list.

Code:

>>> def acceptable(s):
...    return s in ["FOO", "BAR", "BAZ", "QUUX"]
...
>>> for s in ["foo", "foO", "Foo", "bAR", "asdf", "QuUx"]:
...    print "%s: %s" % (s, acceptable(s.upper()))
...
foo: True
foO: True
Foo: True
bAR: True
asdf: False
QuUx: True



All times are GMT -5. The time now is 04:20 AM.