LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-29-2003, 06:28 PM   #1
Odd_Bloke
Member
 
Registered: Aug 2003
Location: P'boro, UK
Distribution: Fedora Core 1 (Yarrow)
Posts: 87

Rep: Reputation: 15
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
 
Old 08-30-2003, 03:58 AM   #2
nouse66
Member
 
Registered: Feb 2003
Location: O.C., CA
Distribution: Kubuntu
Posts: 70

Rep: Reputation: 15
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()
 
Old 09-02-2003, 04:20 PM   #3
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to disable case sensitivity in mysql query in Fedora Linux Core 3 radatan71 Linux - Software 1 10-28-2005 11:39 PM
Converting sLoPPy cASE to Pretty Case with tr lowpro2k3 Programming 4 04-13-2005 08:13 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 04:09 PM
Activating case sensitivity in `sort' command? wirawan0 Linux - General 4 06-26-2004 10:20 AM
Case sensitivity in SED substitutions jkoch Programming 1 02-21-2003 04:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration