473,506 Members | 16,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1867
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12490
by: Nuno | last post by:
Is there any SQL Error? Or I have to use Select case in VB code to control SQL instead. Thank you for any ans. Nuno
10
2588
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
43
2680
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
6
1495
by: HappyHippy | last post by:
More of a minor niggle than anything but how would I remove the aforementioned space? eg. strName = 'World' print 'Hello', strName, ', how are you today?' comes out as "Hello World , how are...
4
71855
by: Kevin Blount | last post by:
I've been unsuccessful finding a site that tells me, so I'm asking here: can I have multiple 'clauses' per 'case' in a 'switch' statement? e.g. switch "myVar" { case "1", "a": break;
7
3077
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
4
3872
by: fran7 | last post by:
Hi, from help in the javascript forum I found the error in some code but need help. This bit of code works perfectly, trouble is I am writing it to a javascript function so the height needs to be in...
11
1995
by: bcurtu | last post by:
Hi, I have a BIIIIIG problem with the next query: cursor.execute(""" SELECT titem.object_id, titem.tag_id FROM tagging_taggeditem titem WHERE titem.object_id IN (%s) """,( eid_list))
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.