By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.