Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Case Sensitivity
#1
Hello, this is my first post. I'm a complete newbie in coding and I ran into a brick wall. I need to make the comparison between two lists case insensitive and no idea how to incorporate it into the code. I know it has to do something with .lower() but no idea how. I would appreciate the help. Thanks
current_users=['sandra', 'brigita', 'bri','deivis', 'raimis']
new_users=['bri', 'billy', 'minde', 'erika', 'edminrika', 'RAIMIS']
for new_user in new_users:
    if new_user in current_users:
        print(new_user+" "+"already exists."+" "+"You need to choose a different name.\n")
    else:
        print(new_user+" "+"is available.")
Reply
#2
Assuming your current_users list is all lowercase. You just need to convert your new_user name into lowercase before checking if it's in the current_users list.

You can do that with .lower() function.
current_users=['sandra', 'brigita', 'bri','deivis', 'raimis']
new_users=['bri', 'billy', 'minde', 'erika', 'edminrika', 'RAIMIS']
for new_user in new_users:
    if new_user.lower() in current_users:
        print(new_user+" "+"already exists."+" "+"You need to choose a different name.\n")
    else:
        print(new_user+" "+"is available.")
Reply
#3
Every string object has a str.lower() method which you can use to convert all letters to lowercase

victor ~ $ python
Python 3.6.2 (default, Jul 20 2017, 03:52:27)
[GCC 7.1.1 20170630] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> "D. Trump".lower()
'd. trump'
>>>
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Simple yet so complicated :) Thank you very much. Sorry about the tag.
Reply
#5
>>> dir("D. Trump")
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Here are all methods of the object "D. Trump". You can use all of them just like lower() method. Get 'swapcase' for example:
>>> my_string = "D. Trump"
>>> my_string.swapcase()
'd. tRUMP'
>>> "D. Trump".swapcase() # or directly.
'd. tRUMP'
>>>
Everything in Python is an object. Strings too. This object has __method__ methods for its own usage and regular ones. You can use all of them or overwrite each one.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Switch case or match case? Frankduc 9 4,484 Jan-20-2022, 01:56 PM
Last Post: Frankduc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020