470,638 Members | 1,617 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,638 developers. It's quick & easy.

Is there any way to say ignore case with "in"?

Is there any way in python to say

if string1 in string2:
<do something>

ignoring the case of string1 and string2?
I know I could use:-

if lower(string1) in lower(string2):
<do something>

but it somehow feels there ought to be an easier (tidier?) way.

--
Chris Green
Apr 4 '08 #1
5 1772
ti*****@isbd.co.uk wrote:
Is there any way in python to say

if string1 in string2:
<do something>

ignoring the case of string1 and string2?
if string1.lower() in string2.lower():
...

(there's no case-insensitive version of the "in" operator in stock Python)

</F>

Apr 4 '08 #2
Hallöchen!

ti*****@isbd.co.uk writes:
Is there any way in python to say

if string1 in string2:
<do something>

ignoring the case of string1 and string2?
You can "normalise" both first, i.e. converting to lower case.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for further contact info.)
Apr 4 '08 #3
On Apr 4, 2:43*pm, tinn...@isbd.co.uk wrote:
Is there any way in python to say

* * if string1 in string2:
* * * * <do something>

ignoring the case of string1 and string2?

I know I could use:-

* * if lower(string1) in lower(string2):
* * * * <do something>

but it somehow feels there ought to be an easier (tidier?) way.
Easier? You mean like some kind of mind meld?

Apr 5 '08 #4
7stud wrote:
On Apr 4, 2:43 pm, tinn...@isbd.co.uk wrote:
>Is there any way in python to say

if string1 in string2:
<do something>

ignoring the case of string1 and string2?

I know I could use:-

if lower(string1) in lower(string2):
<do something>

but it somehow feels there ought to be an easier (tidier?) way.

Easier? You mean like some kind of mind meld?
That's right, DWIM mode Python. Rock on!

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Apr 5 '08 #5
Mel
Paul McGuire wrote:
On Apr 6, 8:53 am, "Martin v. Löwis" <mar...@v.loewis.dewrote:
>>>I know I could use:-
if lower(string1) in lower(string2):
<do something>
but it somehow feels there ought to be an easier (tidier?) way.
Take, for example, U+017F, LATIN SMALL LETTER LONG S. It's .lower() is
the same character, as the character is already in lower case.
It's .upper() is U+0053, LATIN CAPITAL LETTER S. Notice that the LONG
is gone - there is no upper-case version of a "long s".
It's .upper().lower() is U+0073, LATIN SMALL LETTER S.

So should case-insensitive matching match the small s with the small
long s, as they have the same upper-case letter?
[ ... ]
>>>[i for i in range(65536) if unichr(i).lower().upper() !=
... unichr(i).upper()]
[304, 1012, 8486, 8490, 8491]

Instead of 15 exceptions to the rule, conversion to upper has only 5
exceptions. So perhaps comparsion of upper's is, while not foolproof,
less likely to encounter these exceptions? Or at least, simpler to
code explicit tests.
I don't know what meaning is carried by all those differences in
lower-case glyphs. Converting to upper seems to fold together a lot
of variant pi's and rho's which I think would be roughly a good thing.
I seem to recall that the tiny iota (ypogegrammeni) has or had
grammatical significance. The other effect would be conflating
physics' Angstron unit and Kelvin unit signs with ring-a and K.
Applicaton programmers beware.

Mel.
Apr 7 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by LaEisem | last post: by
43 posts views Thread by markryde | last post: by
4 posts views Thread by Kevin Blount | last post: by
1 post views Thread by Korara | last post: by
???
1 post views Thread by Stoney L | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.