How to make case insensitive? [closed]

2024/7/8 8:19:39
int_string = input("What is the initial string? ")
int_string = int_string.lower()

How do I make the input case insensitive

Answer
class CaseInsensitiveStr(str):def __eq__(self, other):return str.__eq__(self.lower(), other.lower())def __ne__(self, other):return str.__ne__(self.lower(), other.lower())def __lt__(self, other):return str.__lt__(self.lower(), other.lower())def __gt__(self, other):return str.__gt__(self.lower(), other.lower())def __le__(self, other):return str.__le__(self.lower(), other.lower())def __ge__(self, other):return str.__ge__(self.lower(), other.lower())int_string = CaseInsensitiveStr(input("What is the initial string? "))

If you don't like all the repetitive code, you can utilise total_ordering to fill in some of the methods like this.

from functools import total_ordering@total_ordering
class CaseInsensitiveMixin(object):def __eq__(self, other):return str.__eq__(self.lower(), other.lower())def __lt__(self, other):return str.__lt__(self.lower(), other.lower())class CaseInsensitiveStr(CaseInsensitiveMixin, str):pass

Testcases:

s = CaseInsensitiveStr("Foo")
assert s == "foo"
assert s == "FOO"
assert s > "bar"
assert s > "BAR"
assert s < "ZAB"
assert s < "ZAB"
https://en.xdnf.cn/q/120649.html

Related Q&A

Webpage data extraction automation using python and selenium webdriver [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Asking the user if they want to play again [duplicate]

This question already has answers here:Ask the user if they want to repeat the same task again(2 answers)Closed 5 years ago.Basically its a guessing game and I have literally all the code except for th…

IF statement (checking for a string in a list) behave weirdly [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 10 years ago.This will probably be a dumb question, but why does this piece…

filteration in txt file in python

I have too many lines like this:>ENSG00000100206|ENST00000216024|DMC1|2371|38568257;38570043|38568289;38570286 CTCAGACGTCGGGCCGACGCAAGGCCACGCGCGCGAACACACAGGTGCGGCCCCGGGCCA CACGCACACCGTACAC >ENSG0…

Count the number of times elements in a numpy array consecutively satisfy a condition

I have a numpy array as follows:import numpy as np a = np.array([1, 4, 2, 6, 4, 4, 6, 2, 7, 6, 2, 8, 9, 3, 6, 3, 4, 4, 5, 8])and a constant number b=6I am searching for a number c which is defined by t…

Finding the index of the first repeating item in an array in python

I was solving an arrays problem from GFG, where we need to find the index of the first repeating element. The question is as follows: Given an array arr[] of size n, find the first repeating element. T…

how to check str int list and tuple? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to take out numbers and add them together in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Questions asking for code must demonstrate a minimal understanding of the problem being solved. Incl…

Populate mysql table with random data in python

How can create a mysql table in python and then populate it with random data.I want around 10000 rows and integer values will do work.

I want to change a tuple into string without joining it in python. How could I do that? [duplicate]

This question already has answers here:Convert a list to a string without join(5 answers)Closed 3 years ago.For Example: I want to change t=(a, b, c) to s=a, b, c