Jump to content

Removing Case Sensitivity in Python?

Go to solution Solved by 79wjd,
8 minutes ago, Red Saum said:

Okay. Is there not a way to make the whole "concept" of cases disappear from the program, rather than make it just "not matter"? Maybe I'm just playing around something that doesn't exist...

You could overload the equality operator within a class so that you could just do if(string1 == string2), but in the overload you'd have to do something like string.uppercase().

How can I remove case sensitivity for the purpose of this little exercise?

 

names = ['Ashton', 'Paris', 'Britton', 'QUIT', 'Aaron']
for name in names:
	if name == 'QUIT':
    	break
    print(name)

I'm trying to make it so I can say name == 'quit' or something without having to worry about the case. Not really useful or necessary, just curious about it.

Link to comment
https://linustechtips.com/topic/875228-removing-case-sensitivity-in-python/
Share on other sites

Link to post
Share on other sites

string = string.upper() will make it uppercase.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

1 minute ago, Red Saum said:

Well I know that, but what if I just want to make the case not matter? Like I said, it's not really necessary, I'm just curious about it...

if(string1.upper() == string2.upper()):

 

It doesn't change the string, it just compares an uppercase version of the string. It's making the case not matter. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

4 minutes ago, djdwosk97 said:

if(string1.upper() == string2.upper()):

 

It doesn't change the string, it just compares an uppercase version of the string. It's making the case not matter. 

Okay. Is there not a way to make the whole "concept" of cases disappear from the program, rather than make it just "not matter"? Maybe I'm just playing around something that doesn't exist...

Link to post
Share on other sites

6 minutes ago, Red Saum said:

Okay. Is there not a way to make the whole "concept" of cases disappear from the program, rather than make it just "not matter"? Maybe I'm just playing around something that doesn't exist...

Concept of cases is actually locked in to the actual string comparison
No real easy way to redefine it.

But as @Red Saum & @djdwosk97 suggested, just uppercase both strings in the comparison. It will be case insensitive

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to post
Share on other sites

4 minutes ago, Red Saum said:

Okay. Is there not a way to make the whole "concept" of cases disappear from the program? Maybe I'm just playing around something that doesn't exist...

This IS the way to make "case" disappear...

You make them both the same case, either upper or lower and compare them.

That's literally what allows you to compare things with different cases while disregarding the case.

.upper, .lower, .casefold, you can use any of these to get what you want.

This is how programming works.

 

If you magically make case disappear then how would other people use the program if they wanted to have case sensitive stuff...

It's simply not possible to make case disappear, all keyboards and computers have upper and lower case. That's what ASCII is, unless you want to invent your own character code that has no case.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to post
Share on other sites

8 minutes ago, Red Saum said:

Okay. Is there not a way to make the whole "concept" of cases disappear from the program, rather than make it just "not matter"? Maybe I'm just playing around something that doesn't exist...

You could overload the equality operator within a class so that you could just do if(string1 == string2), but in the overload you'd have to do something like string.uppercase().

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×