Problem Formulation and Solution Overview
For example, the following three (3) Strings are not identical. If compared with each other, they would return False.
when life gives you lemons, make lemonade. |
This is because each character in the ASCII Table assigns different numeric values for each key or key combination on the keyboard.
This article outlines various ways to ignore the case of Strings.
Method 1: Use lower() and a lambda
This method uses lower()
and a lambda
to convert a List
of Strings to lower case to search for an Employee.
staff = ['Andy', 'MAci', 'SteVe', 'DarCY', 'Harvey'] staff_lower = list((map(lambda x: x.lower(), staff))) print(staff_lower)
Above declares a List
of Rivers Clothing Employees. If we were searching for an Employee, such as Darcy
, using this List, no results would return as Darcy
and DarCY
are not the same. Let’s fix this issue.
ℹ️ Info: A l
a
mbda
function is a one-liner that, in this case, converts each List element in staff
to lower case. Then, converts to a map()
object and back to a List. The output saves to staff_lower
and is output to the terminal.
['andy', 'maci', 'steve', 'darcy', 'harvey'] |
If a comparison was made now, it would find the correct Employee.
if ('Darcy'.lower() == staff_lower[3]): print('Match Found!')
Match Found! |
Method 2: Use upper() and List Comprehension
This method uses upper()
and a list comprehension to convert a List of Strings to upper case and search for an Employee.
brands = ['ChaNnel', 'CLINique', 'DIOR', 'Lancome', 'MurAD'] brands_upper = [x.upper() for x in brands] print(brands_upper)
Above declares a List of the Top five (5) Beauty Brands in the world. If we were searching for a Company, such as Channel
, using this List, no results would return as
and Channel
ChaNnel
are not the same. Let’s fix this issue.
A List Comprehension is called and converts each element in brands
to upper case. The results are saved to brands_upper
and output to the terminal.
['CHANEL', 'CLINIQUE', 'DIOR', 'LANCOME', 'MURAD'] |
If a comparison was made now, it would find the correct Brand.
my_fav = 'Murad' if (my_fav.upper() == brands_upper[4]): print(f'{my_fav} Found!')
Murad Found! |
Method 3: Use title()
This method uses title()
to convert each word in a String to upper case. Great use of this is to convert a Full Name to title case. This ensures both the First, Middle and Last Names are as expected.
full_name = 'jessica a. barker'.title() print(full_name)
Above accepts a string, appends the title()
method to the string, saves it to full_name
and outputs to the terminal.
Jessica A. Barker
Method 4: Use casefold() and a Dictionary
This method uses casefold()
to convert a String to lower case. This method does more than lower()
: it uses Unicode Case Folding Rules.
phrase = 'Nichts ist unmöglich für den Unwilligen!' cfold = phrase.casefold() print(cfold)
Above creates a quote in German and saves it to phrase
. This ensures all characters are correctly converted to lower case, thus making a caseless match.
nichts ist unmöglich für den unwilligen! |
What does this say?
Method 5: Use lower() and Dictionary Comprehension
This method uses Use lower()
to convert Dictionary values into lower case using Dictionary Comprehension.
employees = {'Sandy': 'Coder', 'Kevin': 'Network Specialist', 'Amy': 'Designer'} result = {k: v.lower() for k, v in employees.items()} print(result)
Above creates a Dictionary containing Rivers Employees Names and associated Job Title. This saves to employees
.
Next, Dictionary Comprehension is used to loop through the values in the key:value Dictionary pairs and convert the values to lower case. The output saves to result
and is output to the terminal.
{'Sandy': 'coder', 'Kevin': 'network specialist', 'Amy': 'designer'} |
Bonus: CSV Column to title() case
In this Bonus section, a CSV file you are working with contains a column of Full Names. You want to ensure all data entered in this column is in title()
case. This can be accomplished by running the following code.
Contents of finxter_users.csv
file
FID | Full_Name |
30022145 | sally jenkins |
30022192 | joyce hamilton |
30022331 | allan thompson |
30022345 | harry stiller |
30022157 | ben hawkins |
import pandas as pd users = pd.read_csv('finxter_users.csv') users['Full_Name'] = users['Full_Name'].str.title() print(users)
Above imports the Pandas library. For installation instructions, click here.
Next, the finxter_users.csv
file (contents shown above) is read and saved to the DataFrame users
.
The highlighted line directly accesses all values in the users['Full_Name']
column and converts each entry to title()
case. This saves back to users['Full_Name']
and is output to the terminal.
FID | Full_Name | |
0 | 30022145 | Sally Jenkins |
1 | 30022192 | Joyce Hamilton |
2 | 30022331 | Allan Thompson |
3 | 30022345 | Harry Stiller |
4 | 30022157 | Ben Hawkins |
💡Note: Don’t forget to save the DataFrame to keep the changes.
Summary
Programmer Humor
❓ Question: Why do programmers always mix up Halloween and Christmas?
❗ Answer: Because Oct 31 equals Dec 25.
(If you didn’t get this, read our articles on the oct()
and int()
Python built-in functions!)

At university, I found my love of writing and coding. Both of which I was able to use in my career.
During the past 15 years, I have held a number of positions such as:
In-house Corporate Technical Writer for various software programs such as Navision and Microsoft CRM
Corporate Trainer (staff of 30+)
Programming Instructor
Implementation Specialist for Navision and Microsoft CRM
Senior PHP Coder