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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tinnews@isbd.co.uk

    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
  • Fredrik Lundh

    #2
    Re: Is there any way to say ignore case with &quot;in&quo t;?

    tinnews@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>

    Comment

    • Torsten Bronger

      #3
      Re: Is there any way to say ignore case with &quot;in&quo t;?

      Hallöchen!

      tinnews@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: bronger@jabber. org
      (See http://ime.webhop.org for further contact info.)

      Comment

      • 7stud

        #4
        Re: Is there any way to say ignore case with &quot;in&quo t;?

        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?

        Comment

        • Steve Holden

          #5
          Re: Is there any way to say ignore case with &quot;in&quo t;?

          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/

          Comment

          • Mel

            #6
            Re: Is there any way to say ignore case with &quot;in&quo t;?

            Paul McGuire wrote:
            On Apr 6, 8:53 am, "Martin v. Löwis" <mar...@v.loewi s.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.

            Comment

            Working...