Back to articles list Articles
6 minutes read

Is Python Case-Sensitive?

Learn about case sensitivity in Python.

When learning a new programming language, one of the most basic things you think of is whether it is case-sensitive. Python is no exception – case sensitivity is an important factor. You are probably wondering whether Python is case-sensitive if you’re new to the language. Let’s find out!

Yes, Python Is a Case-Sensitive Language

First, let’s clarify what case sensitivity is. It’s the differentiation between lower- and uppercase letters. It can be a feature not only of a programming language but of any computer program.

The shortest answer to the question of case sensitivity in Python is yes. It is a case-sensitive language, like many other popular programming languages such as Java, C++, and JavaScript. Case sensitivity in Python increases the number of identifiers or symbols you can use.

We explore the different aspects of Python as a case-sensitive language in this article.

Case-Sensitive Names in Python

The most common example of case sensitivity in Python is the variable names. username, UserName, and userName are three different variables, and using these names interchangeably causes an error. The same rule applies to function names.

>>> user_name = 'User1'
>>> print(User_name)

The code above causes an error because of the inconsistency in upper- and lowercase letters in the variable names:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'User_name' is not defined

You can see the correct usage of case-sensitive variable names in the example below:

>>> user_name = 'User2'
>>> print(user_name)

To avoid problems with case-sensitive functions and variable names, use lowercase names with underscores between words for readability (e.g., user_name) as stated in the official Python documentation. You can practice that in our Python Basics track or learn more about Python best practices if you’re already familiar with the basics.

Names of constants in Python are an exception to these naming conventions. They are often in uppercase so that you can tell a constant from a variable easily. Classes are another story – their names are usually written in Pascal-case, which means every word starts with a capital letter. No underscores should be used in class names: e.g., AppFactory.

Python was designed to be highly readable, and it’s important to keep it that way. Avoid confusion in your code by using consistent naming conventions and by avoiding names that are hard to distinguish from one another (like the uppercase letter 'I' and the lowercase 'l'). Use descriptive names but keep them as short as possible.

Case-Sensitive Python Keywords

Keywords are another important part of Python syntax that is case-sensitive. Just a quick reminder: the keywords in Python are special words that have a certain meaning to the interpreter. Their usage is restricted; you can’t use them as variable or function names.

for i in range(1, 10):
	if i == 5:

As you can see in the example code above (the Python keywords are in bold), the majority of Python keywords are lowercase. Other common keywords are def, import, is, not, return, and with, but there are many more.

There are some exceptions – only three, actually. They are True, False, and None.

Even the smallest case changes in Python keywords cause an error like the example below:

>>> For i in range(1, 10):
  File "<stdin>", line 1
    For i in range(1, 10):
SyntaxError: invalid syntax

You can practice all of the most common Python keywords on, especially in the Python Basics and Learn Programming with Python tracks.

Can We Make Python Case-Insensitive?

There are times when it would be easier if Python were case-insensitive. Imagine a situation when customers are searching for a certain product in an online store. Let’s say they are interested in Finnish designs and look for Alvar Aalto’s vase. What do they type in the search box? Maybe: “Alvar Aalto vase”, but most probably “alvar aalto vase”. Either way, they need to return the same search results.

We need to consider case sensitivity in Python when comparing strings. But don’t worry! Python is a multi-purpose programming language and has useful built-in methods to make programmers’ life easier. This is true also when it comes to case-insensitive comparisons.

Approach No 1: Python String lower() Method

This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings. The example code shows how the lower() method works.

english_eels = 'My Hovercraft Is Full of Eels'
other_english_eels = 'My HoVeRcRaFt Is FuLl Of EeLs'

if english_eels.lower() == other_english_eels.lower():
	print('Identical strings:')
	print('1.', english_eels.lower())
	print('2.', other_english_eels.lower())
	print('Strings not identical')


Identical strings:
1. my hovercraft is full of eels
2. my hovercraft is full of eels

Approach No 2: Python String upper() Method

This method is also popular for case-insensitive comparisons in Python. It changes all characters in a string into uppercase characters. Look at the example code below:

polish_eels = 'Mój poduszkowiec jest pełen węgorzy'
other_polish_eels = 'MóJ pOdUsZkOwIeC jEsT pEłEn WęGoRzY'
if polish_eels.upper() == other_polish_eels.upper():
	print('Identical strings:')
	print('1.', polish_eels.upper())
	print('2.', other_polish_eels.upper())
	print('Strings not identical')


Identical strings:

Approach No 3: Python String casefold() Method

Using the casefold() method is the strongest and the most aggressive approach to string comparison in Python. It’s similar to lower(), but it removes all case distinctions in strings. This is a more efficient way to make case-insensitive comparisons in Python.

german_eels = 'Mein Luftkißenfahrzeug ist voller Aale'
other_german_eels = 'MeIn LuFtKißEnFaHrZeUg IsT vOlLeR AaLe'
if german_eels.casefold () == other_german_eels.casefold ():
	print('Identical strings:')
	print('1.', german_eels.casefold())
	print('2.', other_german_eels.casefold())
	print('Strings not identical')


Identical strings:
1. mein luftkissenfahrzeug ist voller aale
2. mein luftkissenfahrzeug ist voller aale

As you can see in the example code, the casefold() method not only changed all characters to the lowercase but also changed the lowercase 'ß' letter to 'ss'.

If you need to know more about strings, check out this beginner-friendly course about working with strings in Python.

Navigate Python Case Sensitivity With Ease

I hope the most important aspects of Python case sensitivity are no longer a mystery to you. You are now familiar with some good case-sensitive naming practices in Python. You now also know how to ignore the case in Python for case-insensitive string comparisons.

So, are you ready for some new Python adventures? Maybe you want to explore what data scientists are using Python for. Or, if you’re just beginning to learn how to code, this article introduces you to programming.