LearnPython.com
  • Courses
  • Articles
  • Log in
  • Create free account
  • fullName

    User profile menu open Open user profile menu avatar
    avatar
    fullName
    Dashboard
    My Profile
    Payment & Billing
    Log out
MENU CLOSE
  • Courses
  • Articles
  • Dashboard
  • My Profile
  • Payment & Billing
  • Log in
  • Create free account
  • Log out 
Back to articles list Articles
17th Mar 2022 6 minutes read

Is Python Case-Sensitive?

Author's photo
Magdalena Wojtas
  • python
See More

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)
User2

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:
		continue
	print(i)

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 LearnPython.com, 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())
else:
	print('Strings not identical')

Output:

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())
else:
	print('Strings not identical')

Output:

Identical strings:
1. MÓJ PODUSZKOWIEC JEST PEŁEN WĘGORZY
2. MÓJ PODUSZKOWIEC JEST PEŁEN WĘGORZY

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())
else:
	print('Strings not identical')

Output:

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.

Tags:

  • python

You may also like

Python Coding Best Practices and Style Guidelines
You've spent hours studying Python, and you may even have several successful projects in your portfolio. But do you write your Python code like a pro? Let's review some important guidelines to help you clean up your code.
Read more
Who Are Data Scientists and What Do They Use Python For?
Find out what data science is, what data scientists do, and what skills you need to become one and be successful.
Read more
How To Start Your Adventure With Programming
Programming is not as difficult as it might look from the outside. Do you want to start your programming adventure? Here’s how.
Read more
Did You Lose Your Job During the Pandemic? Start Learning Python Programming!
The pandemic took its toll on everyone. Have you lost your job due to COVID-19? Maybe this is a good time to learn Python and start programming.
Read more
Coding Wo[men]'s World: How to Start Coding
Prejudice and fear are often the reasons why people never start coding. In this article, you will read the stories of women who have overcome those concerns and learned how to code.
Read more
Git and GitHub – a Beginner Friendly Overview
With this beginner-friendly and quick Git tutorial, you'll learn how to create a remote repository and track changes in your projects.
Read more
Subscribe to our newsletter Join our monthly newsletter to be notified about the latest posts.

How Do You Write a SELECT Statement in SQL?

What Is a Foreign Key in SQL?

Enumerate and Explain All the Basic Elements of an SQL Query

Quick links

  • Pricing
  • Blog
  • Vertabelo.com

Assistance

Need assistance? Drop us a line at [email protected]

Write to us

Follow us

LearnSQL Facebook We Learn SQL Facebook Linkedin LearnPython.com We Learn SQL Youtube
go to top
Copyright ©2016-2018 Vertabelo SA All rights reserved
Vertabelo
  • Terms of service
  • Privacy policy
  • Imprint